PostgreSQL – CEIL Function

In PostgreSQL, the CEIL() function is used to round a numeric value up to the nearest integer that is greater than or equal to the original value. It is also known as the “ceiling” function.

The basic syntax of the CEIL() function is as follows:

CEIL(numeric_expression)

where numeric_expression is the numeric value or expression that you want to round up to the nearest integer.

Below are some examples of using the CEIL() function:

  1. Rounding up a positive decimal number:
SELECT CEIL(5.7) AS rounded_value;

This query will return 6 since the nearest integer greater than or equal to 5.7 is 6.

  1. Rounding up a negative decimal number:
SELECT CEIL(-3.2) AS rounded_value;

This query will return -3 since the nearest integer greater than or equal to -3.2 is -3.

  1. Rounding up an expression:
SELECT CEIL(10 / 3) AS rounded_value;

This query will return 4 since the nearest integer greater than or equal to 10 / 3 (which is approximately 3.3333) is 4.

The CEIL() function is useful when you need to ensure that a value is rounded up to the nearest integer, regardless of whether it is positive or negative.

CEIL() function returns the result as a numeric data type. If you need the result in a different data type, you should explicitly cast it to the desired type using the CAST() or :: operator. For example:

SELECT CEIL(5.7)::integer AS rounded_integer;

This query will return 6 as an integer data type.

Similar Posts