SQL Server CEILING() Function
In SQL Server, CEILING()
is a mathematical function used to round a number up to the nearest integer.
Syntax
The syntax for CEILING()
function is as follows:
CEILING ( numeric_expression )
Here, numeric_expression
is the numeric expression to be rounded up.
Usage
The CEILING()
function is commonly used to round up values that require rounding up, such as prices, quantities, times, and so on.
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 column named TotalAmount
, and we want to round up the total amount of all orders to the nearest $10. We can use the following query:
SELECT CEILING(TotalAmount/10)*10 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
The CEILING()
function is a very useful mathematical function that can round a number up to the nearest integer or specified radix. It is suitable for many scenarios, such as computing prices, quantities, times, and so on.