In MySQL, the CEIL function is used to return the smallest integer value that is greater than or equal to a specified numeric expression or column. It rounds up the given value to the nearest integer greater than or equal to the original value. The CEIL function is particularly useful when you need to ensure that a value is always rounded up to the next whole number.
Syntax
Here is the basic syntax of the CEIL function:
CEIL(number);
number: The numeric expression or column that you want to round up to the nearest integer.
Example
Let’s look at a few examples to illustrate how the CEIL function works:
SELECT CEIL(5.7); -- Output: 6 SELECT CEIL(9.1); -- Output: 10 SELECT CEIL(-3.4); -- Output: -3 SELECT CEIL(0); -- Output: 0
In the first example, CEIL(5.7) returns 6 because it rounds up the decimal value 5.7 to the nearest integer, which is 6.
In the second example, CEIL(9.1) returns 10 because it rounds up the decimal value 9.1 to the nearest integer, which is 10.
In the third example, CEIL(-3.4) returns -3 because it rounds up the negative decimal value -3.4 to the nearest integer, which is -3.
In the last example, CEIL(0) returns 0 because there is no need to round up the value 0.
It’s important to note that the CEIL function can be particularly useful in scenarios where you need to ensure that a quantity, such as a result of a mathematical calculation or a measurement, is always rounded up to the nearest whole number, regardless of the decimal component.