How to SELECT round up number in SQL Server

前端 未结 5 1863
南方客
南方客 2021-01-25 08:44

I have a salary table with this column

EMPLOYEE_NAME    SALARY
------------------------
ANNA             113750
MARRY            124300
BELLA            105100
<         


        
相关标签:
5条回答
  • 2021-01-25 08:58

    You can use the CEILING function.

    Like:

    SELECT EMPLOYEE_NAME CEILING(SALARY)
    

    Read this for further details

    Short summary from the above link:

    In SQL Server (Transact-SQL), the CEILING function returns the smallest integer value that is greater than or equal to a number.

    0 讨论(0)
  • 2021-01-25 09:00

    Divide the salary by the fraction. Use ceiling to round up. Then multiply by the fraction.

    Declare @salary table (Employee_name nvarchar(50), Salary money)
    Declare @fraction money = 5000
    
    insert into @salary
    values
    ('ANNA',             113750),
    ('MARRY',           124300),
    ('BELLA',           105100)
    
    update @salary
    set Salary = ceiling(salary/@fraction)*@fraction
    
    select * from @salary
    
    0 讨论(0)
  • 2021-01-25 09:07

    Try below CEILING method :

      DECLARE @fraction MONEY = 1000
    
       SELECT CEILING(113750/@fraction)*@fraction 
       SELECT CEILING(124300/@fraction)*@fraction
       SELECT CEILING(105100/@fraction)*@fraction 
    
    0 讨论(0)
  • 2021-01-25 09:17

    Unlike ROUND (which rounds up and down) the CELING function (which you want to round up) has no length parameter (as in ROUND(salary, -3)). So divide and multiply to get the desired result:

    select employee_name, ceiling(salary / 1000.0) * 1000.0 as salary
    from mytable;
    
    • CEILING is documented here: https://msdn.microsoft.com/de-de/library/ms189818.aspx
    • ROUND is documented here: https://msdn.microsoft.com/de-de/library/ms175003.aspx
    0 讨论(0)
  • 2021-01-25 09:21

    try this.

    select EMPLOYEE_NAME, ceiling(salary / 1000.0) * 1000.0 as SALARY from table;
    
    0 讨论(0)
提交回复
热议问题