为什么SQL Server不返回0进行计算

时间:2016-06-28 04:55:04

标签: sql sql-server

我有以下查询:

select 
    (case 
        when try_convert(decimal(3, 1), DEC10assessmentData.writ_literatureReview) is not null 
           then cast(DEC10assessmentData.writ_literatureReview as decimal(3, 1)) 
           else 0 
     end / 100 * 4
     + case 
          when try_convert(decimal(3, 1), DEC10assessmentData.writ_exposition) is not null 
             then cast(DEC10assessmentData.writ_exposition as decimal(3, 1)) 
             else 0 
       end / 100 * 8
     + case 
          when try_convert(decimal(3, 1), DEC10assessmentData.writ_groupReport) is not null 
            then cast(DEC10assessmentData.writ_groupReport as decimal(3, 1)) 
            else 0 
       end / 100 * 8
     + case 
          when try_convert(decimal(3, 1), DEC10assessmentData.writ_synthSummary) is not null 
            then cast(DEC10assessmentData.writ_synthSummary as decimal(3, 1)) 
            else 0 
       end / 100 * 8
     + case 
          when try_convert(decimal(3, 1), DEC10assessmentData.writ_critEvaluation) is not null 
             then cast(DEC10assessmentData.writ_critEvaluation as decimal(3, 1))  
             else 0 
       end / 100 * 12) / 40 * 100
from 
    DEC10assessmentData

当所选列中的所有值均为0.800000000时,为什么会返回NULL

当列有分数时,它会返回正确的结果。

1 个答案:

答案 0 :(得分:2)

我尝试在这里创建演示,但我得到的是0.0000,而不是0.8000。您确定所有值都是null吗?

Null Demo

如果是,那么单独运行每个case语句并查看你的输出中的0没有得到输出。

首先运行

select 
(case when try_convert(decimal(3,1), DEC10assessmentData.writ_literatureReview) 
is not null
   then 
cast(DEC10assessmentData.writ_literatureReview as decimal(3,1)
     ) else 0 
 end / 100 * 4
 )/ 40 * 100
from DEC10assessmentData

如果仅返回0,则添加另一个案例陈述并进一步检查。