简体   繁体   English

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

[英]Why Does SQL Server Not Return 0 for Calculation

I have the following query: 我有以下查询:

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

Why does it return 0.800000000 when all values in the selected columns are NULL ? 当选定列中的所有值均为NULL时,为什么返回0.800000000

It returns correct results when columns have scores. 当列具有分数时,它将返回正确的结果。

I tried creating demo here but I am getting 0.0000 , not 0.8000 . 我尝试在此处创建演示,但得到的是0.0000 ,而不是0.8000 Are you sure that all values are null ? 您确定所有值都为null吗?

Null Demo

If yes, the try running each case statement alone and see from which you are are not getting 0 in output. 如果是,请尝试单独运行每个case语句,看看从哪个输出中您不会得到0

So first run 所以先跑

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

If this return only 0 then add another case statement and check further. 如果仅返回0则添加另一个case语句并进一步检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM