SQL Server-始终根据条件向上或向下取整

时间:2019-01-16 16:49:04

标签: sql-server

我希望根据when语句的大小强制舍入或舍入,而不考虑整数。例如,对于一个条件,1.2345变为1.234,而对于另一个条件,则变为1.235。请参阅下面的示例SQL块:

select case when action in ('YES') then round(price, 3, 1) 
            when action in ('NO') then round(price, 3) 
            else price end as RoundPrice

我还简要地探讨了CEILINGFLOOR的表达式,但这使我更容易理解整数。预先感谢您的协助!

2 个答案:

答案 0 :(得分:1)

我最终采用了一种利用CEILING函数的round回方式,有关SQL的信息,请参见下文:

case when action in ('YES') then round(price, 3, 1) 
            when action in ('NO') then CEILING(price*1000)/1000 
            else price end as RoundPrice

答案 1 :(得分:0)

您可以执行以下操作:

select case when action in ('YES') then CAST(price AS DECIMAL(10,2)) 
        when action in ('NO') then LEFT(price, 4) 
        else price end as RoundPrice

进行测试:

SELECT CAST(1.1264 AS DECIMAL(10,2))

SELECT LEFT(1.1264,4)

希望有帮助