The CEILING()
function in SQL Server is used to return the smallest integer greater than or equal to a given numeric expression. It effectively rounds a number up to the nearest integer, regardless of whether the number is already an integer.
Syntax:
CEILING(numeric_expression)
- numeric_expression: The number to round up. It can be a column, variable, or any numeric value.
Example Use Cases:
Example 1: Rounding a positive number
SELECT CEILING(4.2) AS Result;
-- Output: 5
Example 2: Rounding a negative number
SELECT CEILING(-4.7) AS Result;
-- Output: -4
Example 3: Using CEILING with a column
If you have a table named Sales
with a column Price
:
SELECT Price, CEILING(Price) AS RoundedPrice
FROM Sales;
This query will display the original price along with its ceiling value.
Example 4: CEILING with division
SELECT CEILING(10.0 / 3) AS Result;
-- Output: 4
Key Points:
- It always rounds up, even for negative numbers.
- If the input is already an integer, it remains unchanged.
- Can be used in financial, mathematical, or statistical queries where rounding up is needed.
No comments:
Post a Comment