Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round *UP* to the nearest 100 in SQL Server

Is it possible to easily round a figure up to the nearest 100 (or 1000, 500, 200 etc.) in SQL Server?

So:

720 -> 800
790 -> 800
1401 -> 1500

like image 893
joshcomley Avatar asked Jul 08 '10 14:07

joshcomley


People also ask

How do I round up in SQL Server?

SQL Server ROUND() Function The ROUND() function rounds a number to a specified number of decimal places.

How do you round up to the next 100?

When rounding to the nearest hundred, look at the TENS DIGIT of the number. If that digit is 0, 1, 2, 3, or 4, you will round down to the previous hundred. If that digit is 5, 6, 7, 8, or 9, you will round up to the next hundred.

How do I round up a column in SQL?

If you'd like to round a floating-point number to a specific number of decimal places in SQL, use the ROUND function. The first argument of this function is the column whose values you want to round; the second argument is optional and denotes the number of places to which you want to round.


2 Answers

The following should work. After reading your question, I'm not exactly sure what you want 100 to return. For this 100 returns 100.

select floor((X + 99) / 100) * 100; 

This gives the following results:

0 -> 0 1 -> 100 99 -> 100 100 -> 100 101 -> 200 
like image 141
Gray Avatar answered Sep 21 '22 13:09

Gray


For rounding Up to the nearest thousand, try the following:-

select round(YourValue, -3) 
like image 23
rkgit Avatar answered Sep 18 '22 13:09

rkgit