Home > T-SQL > SQL SERVER – CEILING() – Math Functions @SQLSERVER

SQL SERVER – CEILING() – Math Functions @SQLSERVER


Ceiling() function returns the next highest whole number of the given numeric expression. If the numeric expression is negative then it returns the next highest negative whole number. The return data type is same as the argument data type.

Note: if numeric expression contains a decimal number then it will convert to the next greatest whole number and sum up with the integer value else it returns the whole number given in the argument.

Examples

select ceiling (-5.9)
Output: -5
 
select ceiling(5.3)
Output: 6
 
select ceiling(0.00)
Output: 0
 
TSQL code to convert a series of negative and positive  numbers with decimal value using CEILING function:
 
–Convert negative values from -1.0 to -0.9
declare @floatvalue as float = -1.0
declare @i as tinyint = 1
while  @i < 10
begin
set @floatvalue = @floatvalue + 0.1
Print ‘CEILING(‘+cast(@floatvalue as varchar(5))+’)= ‘
Print CEILING(@floatvalue)
set @i = @i +1
end
go
 
–Convert positive values from 1.0 to 1.9
declare @floatvalue as float = 1.0
declare @i as tinyint = 1
while  @i < 10
begin
set @floatvalue = @floatvalue – 0.1
Print ‘CEILING(‘+cast(@floatvalue as varchar(5))+’)= ‘
Print CEILING(@floatvalue)
set @i = @i +1
end
go
 
Output:
Execute this code in SSMS and get the result.
 

Happy programming!!!

Advertisements
  1. No comments yet.
  1. January 1, 2013 at 11:31 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: