首页 > 解决方案 > Is Not Null 参数在 CASE 中给出错误

问题描述

使用 Microsoft SQL Server

select *,
case table1.indthrud when is null then 'Current' when is not null then 'Historical' end Indicator

    from table1

当我尝试运行上述查询时,出现以下错误

关键字“is”附近的语法不正确。

但是下面的查询工作正常:


    select *,
    case  when  table1.indthru is null then 'Current' when  table1.indthru is not null then 'Historical' end Ind
    from table1

你能解释一下为什么论点不能放在 CASE 前面,而必须放在个别的时候吗?

提前致谢!

标签: sqlsql-server

解决方案


首先,您的错误是因为is not null不是一个值。你的第一个case是寻找一个常数进行比较。并且is not null不是一个常数。

其次,当常数为 时null,仍然不匹配,因为null = null对 的评估null在此上下文中被视为假。

你想要的最简单的表达方式是else

select t1.*,
       (case when t1.indthru is null then 'Current' else 'Historical' end) as Ind
from table1 t1

推荐阅读