当结果值为“0.00”时,是否可以将十进制值更改为整数值(0)?

时间:2018-02-06 14:58:55

标签: sql sql-server

每当结果值为“0.00”时,它应显示“0”否则结果。 当我尝试使用case语句时,它显示相同的结果。当结果值为“0.00”时,可以将十进制值更改为整数值吗?

DECLARE @Test TABLE (ID int identity(1,1),Credit decimal(10,2),Debit decimal(10,2),Result decimal(10,2),ExpectedOP VARCHAR(20))
        insert into @Test (Credit,Debit,ExpectedOP)
        select 10,5,'2.00' union
        select 0,2,'0' union
        select 5,6,'0.83'

        update @Test SET Result=Credit/Debit
        select *,CASE WHEN Result=0 THEN 0 ELSE Result END Currentop from @Test

enter image description here

2 个答案:

答案 0 :(得分:0)

您的选项仅限于将其转换为字符串。无论如何,这应该在前端处理。

    select *,CASE WHEN Result=0 THEN CAST(0 as nvarchar(MAX)) ELSE cast(Result as nvarchar(Max)) END Currentop from @Test

答案 1 :(得分:0)

请看这个输出有帮助。 检查newCredit

DECLARE @Test TABLE (ID int identity(1,1),Credit decimal(10,2),Debit decimal(10,2),Result decimal(10,2)
,ExpectedOP VARCHAR(20))

insert into @Test (Credit,Debit,ExpectedOP)
select 10,5,'2.00' union
select 0,2,'0' union
select 5,6,'0.83' union
select 3.09,7,'3.09'

SELECT * , CAST(Credit AS FLOAT) newCredit FROM @test

<强>输出

ID          Credit   Debit      Result      ExpectedOP           newCredit
----------- ------------------- ----------- -------------------- ----------------------
1           0.00     2.00       NULL        0                    0
2           3.09     7.00       NULL        3.09                 3.09
3           5.00     6.00       NULL        0.83                 5
4           10.00    5.00       NULL        2.00                 10

(4 rows affected)