Menu

SQL Server CEILING() Function

Updated on

In SQL Server, CEILING() returns the smallest integer greater than or equal to a numeric expression. For example, CEILING(-123.45) is -123. SQL Server determines the return type from the input; see Microsoft’s CEILING() reference.

Syntax

The syntax for CEILING() function is as follows:

CEILING ( numeric_expression )

Here, numeric_expression is the numeric expression to be rounded up.

Usage

Use CEILING() to round toward positive infinity. It accepts one numeric expression; to round to a multiple such as 10, scale the input and result, as in Example 2.

Return type

CEILING() does not always return an int; SQL Server determines the result type from the input:

Input type Return type
float, real float
decimal(p, s) decimal(p, 0)
int, smallint, tinyint int
bigint bigint
money, smallmoney money
bit float

If the result does not fit in the return type, SQL Server raises an arithmetic overflow error.

Examples

Here are two examples of using the CEILING() function:

Example 1

Suppose we have a products table Products that contains a column named Price, and we want to round up the prices of all products to the nearest integer. We can use the following query:

SELECT CEILING(Price) AS RoundedPrice
FROM Products

Suppose our products table looks like this:

ProductID ProductName Price
1 Product A 10.99
2 Product B 9.50
3 Product C 15.25

The query result will be:

RoundedPrice
11
10
16

Example 2

Suppose we have a sales orders table SalesOrders that contains a numeric column named TotalAmount, and we want to round each amount up to the next multiple of $10. Using 10.0 keeps the division fractional if TotalAmount is an integer type:

SELECT CEILING(TotalAmount / 10.0) * 10.0 AS RoundedAmount
FROM SalesOrders

Suppose our sales orders table looks like this:

OrderID CustomerID TotalAmount
1 100 120.50
2 101 65.00
3 102 89.99

The query result will be:

RoundedAmount
130
70
90

Conclusion

CEILING() returns the smallest integer greater than or equal to its input. It has no scale or radix argument; scale the input explicitly when rounding to a multiple. For the opposite direction, see FLOOR(); for decimal-place rounding, see ROUND().