Tomas Greif
Tomas Greif

Reputation: 22623

Round number UP to multiple of 10

How do I round number UP to the multiple of 10 in PostgreSQL easily?

Example:

In      Out
100 --> 100
111 --> 120
123 --> 130

Sample data:

create table sample(mynumber numeric);

insert into sample values (100);
insert into sample values (111);
insert into sample values (123);

I can use:

select 
 mynumber,
 case
    when mynumber = round(mynumber,-1) then mynumber 
    else round(mynumber,-1) + 10 end as result
from
 sample;

This works well, but looks ugly. Is there simpler way of doing this?

You can find SQLFiddle here

Upvotes: 10

Views: 9875

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125204

select ceil(a::numeric / 10) * 10
from (values (100), (111), (123)) s(a);
 ?column? 
----------
      100
      120
      130

Upvotes: 23

Related Questions