简体   繁体   中英

How to round a number based on the decimal? (SQL)

Hello guys my question is this:

I have a rule to validate if a number is greater or equal than .3 I will round to the next number and add another one. Example:

1000.3 = 1001 + 1 = 1002

If the number is less than .3 I will only add one:

1000.2 = 1001

I have been looking for some kind of command in SQL but I had no luck. Hope you could help me.

Thanks! Ro A.

You can use something like:

SELECT CASE WHEN (Number - FLOOR(Number)) >= .3 
            THEN (CEILING(Number)+1)
            ELSE CEILING(Number)
       END AS NextNumber

I originally wrote that this is ANSI, but it's not. However SQL Server and MySQL both support the % operator.

DECLARE @one DECIMAL(10, 1) = 1000.3

 SELECT  CASE WHEN @one % CAST(@one AS INT) >= 0.3 THEN CAST(@one as INT) + 1
    ELSE CAST(@one as INT) + 2 END
SELECT ROUND(15.193,-1) "Round" FROM DUAL;

     Round
----------
        20 

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