In SQL Server, the T-SQL CEILING()
function allows you to round a number up to the nearest integer. More specifically, it returns the smallest integer greater than, or equal to, the specified numeric expression.
You provide the number as an argument. The return data type is the same as the one provided as an argument.
Syntax
The syntax goes like this:
CEILING ( numeric_expression )
Where numeric_expression is an expression of the exact numeric or approximate numeric data type category.
Note that the bit data type is invalid.
Example 1 – Positive Values
Here’s an example of providing a positive value as the argument.
SELECT CEILING(3.63) Result;
Result:
+----------+ | Result | |----------| | 4 | +----------+
In this case, 4
is the smallest integer not less than 3.63
.
Here’s another example, this time with a larger value.
SELECT CEILING(500.71) Result;
Result:
+----------+ | Result | |----------| | 501 | +----------+
Example 2 – Negative Values
Here’s an example using a negative value.
SELECT CEILING(-3.63) Result;
Result:
+----------+ | Result | |----------| | -3 | +----------+
In this case, -3
is the smallest integer not less than -3.63
.
Here’s another example.
SELECT CEILING(-500.71) Result;
Result:
+----------+ | Result | |----------| | -500 | +----------+
Rounding Down – FLOOR()
If you prefer to round down to the nearest integer, use the T-SQL FLOOR()
function instead.