简体   繁体   中英

Round Up Number Integer in Sql Query

i have Query for round up for my store procedure like this

DECLARE @number int;

SET @number = 918750;

select @number + (100 - @number % 100 )

this is return 918800

the return is correct 918,800

but in case @number = 918800 it will change into 918900.

and i want only 2 last digit is round up.

can i change it into single query only?

yes if i use

SELECT ROUND(918750 , -2) AS RoundValue;

it return 918800, but if i use

SELECT ROUND(918740 , -2) AS RoundValue;

it return 918700, while i need is always round to up so the result i need is 918000

if you re using any of Oracle, MySQL, and PostgreSQL, you can use ROUND() function.

like this

SELECT ROUND(918750 , -2)

this query will produce 918800 result -2 means the last two digits are rounded up.

Try this

DECLARE @number int;

SET @number = 918750;
// Solution 1
select @number -(@number % 100)+iif(@number%100!=0,100,0);

//Solution 2
select ceiling(@number/100.00)*100;

Try this one

DECLARE @Number INT = 435667
SELECT CONVERT(INT, 100 * ROUND(@Number / 100.0, 0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM