Rounding
In SQL, rounding functions are used to manipulate numeric values by adjusting them to a specific precision or to the nearest whole number. Here are some common rounding functions:
-
ROUND:
- The
ROUND
function is used to round a numeric value to a specified number of decimal places. - Syntax:
ROUND(numeric_expression, length [, function])
- Example:
SELECT ROUND(123.456, 2);
returns123.46
- The
-
FLOOR:
- The
FLOOR
function rounds a numeric value down to the nearest integer less than or equal to the original value. - Syntax:
FLOOR(numeric_expression)
- Example:
SELECT FLOOR(123.456);
returns123
- The
-
CEIL:
- The
CEIL
function rounds a numeric value up to the nearest integer greater than or equal to the original value. It’s short for “ceiling”. - Syntax:
CEIL(numeric_expression)
- Example:
SELECT CEIL(123.456);
returns124
- The
-
TRUNCATE:
- The
TRUNCATE
function truncates a numeric value to a specified number of decimal places, effectively removing the decimal part. - Syntax:
TRUNCATE(numeric_expression, length)
- Example:
SELECT TRUNCATE(123.456, 2);
returns123.45
- The