Round *UP* to the nearest 100 in SQL Server

Sql ServerMathRounding

Sql Server Problem Overview


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

Sql Server Solutions


Solution 1 - Sql Server

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

Solution 2 - Sql Server

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

select round(YourValue, -3)

Solution 3 - Sql Server

One option would be to use the CEILING() function like this:

SELECT CEILING(@value/100.0) * 100

You may need to convert your value to a decimal first depending on its type.

Solution 4 - Sql Server

Use CEILING function to round a figure up

DECLARE @Number DECIMAL, @RoundUp DECIMAL
SET @RoundUp = 100
SET @Number = 720
SELECT CEILING(@Number/@RoundUp)*@RoundUp

Solution 5 - Sql Server

Try this:

> select round(@value , -2);

Solution 6 - Sql Server

It is very simple to round a number to any multiple of nearest 10 by using simply the ROUND function for ex:

SELECT ROUND(number/1000,2)*1000 

This will give you the nearest thousandth value.

Solution 7 - Sql Server

This will work for the values with decimal also.

select floor((ceiling (@value) + 99) / 100) * 100;

Solution 8 - Sql Server

There's no native function that will do this, but there are any number of simple math tricks that will. An example:

DECLARE @Foo int
SET @Foo = 720

print @Foo
print (@Foo + 100) % 100
PRINT @Foo - (@Foo + 100) % 100

Solution 9 - Sql Server

You can use this code, assuming your amount is an int. If not you will need to cast, so you get integer division.

If amount % 100 != 0 Then
   roundedAmount = ((amount / 100) * 100) + 100
Else
   roundedAmount = amount

You might want to package this into a user defined function.

Solution 10 - Sql Server

A generic solution - Use MOD to find the last 100th place and then add 100 to the result.

select (720 - MOD(720,100)) + 100 from dual;

If you need the next 80th place, just replace any "100" with "80".

Solution 11 - Sql Server

In addition to [Gray's][1] answer, I'd use the following inline function:

CREATE FUNCTION dbo.udf_RoundNearest
(
@Number bigint,
@RoundNearest bigint,
@Direction int
)

RETURNS TABLE AS 
RETURN

SELECT CASE WHEN @RoundNearest>=@Number THEN @Number
	   ELSE
 		(
				(@Number + CASE 
						   WHEN @Direction = 0 --Round Down
						   THEN 0
						   ELSE CASE WHEN @Number % @RoundNearest = 0 THEN 0 ELSE @RoundNearest END
						   END) / @RoundNearest) * @RoundNearest

   END Number

Parameter Definition:

  1. @Number - the number you need to round
  2. @RoundNearest 10th, 100th , 1000th etc
  3. @Direction 0-> round down, 1-> round up

using the function:

SELECT * FROM dbo.udf_RoundNearest (1965,100,1) --> 2000
SELECT * FROM dbo.udf_RoundNearest (1359,100,0) --> 1300
SELECT * FROM dbo.udf_RoundNearest (1999,10,0) --1990
SELECT * FROM dbo.udf_RoundNearest (80,100,0) --> 80 (if the @number parameter is less or equal the @RoundNearest parameter the result will be the @number itself

it can also be used as apply it versus a table such as:

;with tmp (Value) as
  (select 1236 union all select 6584 union all select 9999)

    select t.*, fn.Number
    from tmp t
 	cross apply dbo.udf_RoundNearest (Value,100,0) fn
  
    /*Result Set
    Value	Number
    1236	1200
    6584	6500
    9999	9900*/

[1]: https://stackoverflow.com/users/179850/gray/ "gray's answer"

Solution 12 - Sql Server

This worked fine for me.

Round(@value/100, 0) * 100

Solution 13 - Sql Server

It works fine for integer value:

@roundUpValue = ((@intValue / 1000) + 1) * 1000
@roundDownValue = (@intValue / 1000) * 1000

For example

declare @intValue as int = 1934
select ((@intValue / 1000) + 1) * 1000 as roundUp
select (@intValue / 1000) * 1000 as roundDown

If you want to round up to the nearest 500 then

select ((@intValue / 500) + 1) * 500 as roundUp

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjoshcomleyView Question on Stackoverflow
Solution 1 - Sql ServerGrayView Answer on Stackoverflow
Solution 2 - Sql ServerrkgitView Answer on Stackoverflow
Solution 3 - Sql ServerkrockView Answer on Stackoverflow
Solution 4 - Sql ServerShriView Answer on Stackoverflow
Solution 5 - Sql ServerSaumyaView Answer on Stackoverflow
Solution 6 - Sql ServerSanjeetView Answer on Stackoverflow
Solution 7 - Sql ServersteaveView Answer on Stackoverflow
Solution 8 - Sql ServerPhilip KelleyView Answer on Stackoverflow
Solution 9 - Sql ServerC. RossView Answer on Stackoverflow
Solution 10 - Sql ServerShaulianView Answer on Stackoverflow
Solution 11 - Sql ServerhkravitzView Answer on Stackoverflow
Solution 12 - Sql ServerCRVView Answer on Stackoverflow
Solution 13 - Sql ServerjorlinskiView Answer on Stackoverflow