PostgreSQL Round UP – CEIL() function

CEIL() Function in PostgreSQL is used to round up the value.

  • Get Round up value in PostgreSQL – CEIL()
  • Create the column which extracts the round up value i.e. ceiling of the column in the PostgreSQL table

 

PostgreSQL Round UP – CEIL() function 1

 

 

 

Syntax for CEIL() Function in PostgreSQL:

SELECT ceil(number_or_expression);

number_or_expression: This is the number or expression for which you want to round up or find the ceiling value to it.

 

 

CEIL() Function in PostgreSQL:

CEIL() function in PostgreSQL returns the round up value, i.e. the smallest integer greater than or equal to a number

Example 1:

 

SELECT CEIL(4.56);

Output:

PostgreSQL Round UP – CEIL() function 2

 

 

Example 2:

 

SELECT CEIL(-3.14);

Output:

PostgreSQL Round UP – CEIL() function 3 

 

 

CEIL() function (Round up value) of the column in PostgreSQL :

you can use ceil() function within a SELECT statement to calculate the smallest integer greater than or equal to each value in the column. Here’s how you can do it with the help of an example

bookkit:

PostgreSQL Round UP – CEIL() function 4

Example 1:

Creating the column which extracts the round up value i.e. Ceiling of the column in the PostgreSQL table is shown

 

select *,CEIL(charges) as ceil_charges from bookkit

  • ”CEIL(charges)” calculates the smallest integer greater than or equal to each charges.
  • ”AS ceil_charges” assigns an alias ”ceil_charges”  to the calculated ceiling value.

So the output will be

Output:

PostgreSQL Round UP – CEIL() function 5

 

 

 

Round up only for the positive valued column in PostgreSQL

Example 1 – using CEIL() function:

We will be rounding up only the positive column in the example let’s take “price” column.

 

select *,ceil(price) as ceil_price from bookkit

Output:

PostgreSQL Round UP – CEIL() function 6

 

 

Example 2 – using CAST() function:

We will be using CAST() function rounding up only the positive column in the example let’s take “price” column.

 

select *,cast(price as integer) as ceil_price from bookkit

The CAST function will get the smallest integer greater than or equal to each value in the column,

Note: This function is applicable only when the entire column has positive Values

Output:

PostgreSQL Round UP – CEIL() function 7

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts