web123456

An example of rounding up, rounding down, rounding rounding in Sql Server!


================================================================= [Round rounding and intercepting]

select round(54.56,0)

=============================================================== [Fetch downwards]

SELECT FLOOR(54.56)

================================================================= [Up to round up and intercept]

 SELECT   CEILING(13.15)


The following is reproduced from: http:///database/201209/

--Usage of MSSQL rounding function

 
--The decimal part will be cut off by dividing two integers
select 3/4,4/3,5/3 
--Result 0, 1, 1
 
--Returns the minimum integer greater than or equal to the given numeric expression
SELECT CEILING(123.55), CEILING(123.45),CEILING(-123.45), CEILING(0.0) 
--Result 124, 124, -123, 0
 --  
--round (a,b) -- The result a is accurate to the right b position of the decimal point, or the left -b position
select round(54.36,-2), round(54.36,-1),round(54.36,0), round(54.36,1),round(54.36,2) 
--Results 100.00, 50.00, 54.00, 54.40, 54.36
 
--- Rounded and converted into integer
select cast(round(56.361,0) as int),cast(round(56.561,0) as int) 
--Result 56, 57
 
--Give examples to use
 
--- Divide two integers and discard the decimal part (all are moving forward)
declare @dividend decimal(20,2), @divisor decimal(20,2) 
 
set @dividend=3 
set @divisor=4 
select CEILING(@dividend/@divisor) 
--Result 1
 
set @dividend=4 
set @divisor=3 
select CEILING(@dividend/@divisor) 
--Result 2
 
set @dividend=5 
set @divisor=3 
select CEILING(@dividend/@divisor) 
--Result 2


--- Divide two integers round to integer
set @dividend=3 
set @divisor=4 
select cast(round(@dividend/@divisor,0) as int) 
--Result 1
 
set @dividend=4 
set @divisor=3 
select cast(round(@dividend/@divisor,0) as int) 
--Result 1
 
set @dividend=5 
set @divisor=3 
select cast(round(@dividend/@divisor,0) as int) 

--Result 2


================================================================= [Round rounding and intercepting]

select round(54.56,0)

=============================================================== [Fetch downwards]

SELECT FLOOR(54.56)

================================================================= [Up to round up and intercept]

 SELECT   CEILING(13.15)