Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

Extended SQL Server Ceiling and Floor functions

Maybe i'm reinventing hot water but no matter. It's fun for me :)))
These 2 functions round the number up or down on the decimal position we specify.

create

function dbo.FloorEx(@num decimal(38, 33), @decimalPosition int)

returns

decimal(38, 33)

as

begin

if

 

@decimalPosition < 0 set @decimalPosition = 0

return

floor(@num*power(10, @decimalPosition))/power(10, @decimalPosition)

end

go

create

function dbo.CeilingEx(@num decimal(38, 33), @decimalPosition int)

returns

decimal(38, 33)

as

begin

if

 

@decimalPosition < 0 set @decimalPosition = 0

return

ceiling(@num*power(10, @decimalPosition))/power(10, @decimalPosition)

end

go

 

declare

select

@num decimal(38, 33), @decimalPosition int @num = 10.123456, @decimalPosition = 3

select

dbo

dbo

go

@num, .FloorEx(@num , @decimalPosition), .CeilingEx(@num , @decimalPosition)

drop

function dbo.FloorEx

drop

function dbo.CeilingEx

Legacy Comments


Christos
2008-01-29
re: Extended SQL Server Ceiling and Floor functions
Thanks, you saved me

Mladen
2008-01-29
re: Extended SQL Server Ceiling and Floor functions
glad you liked it. :)