sql - 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 前面,而必须放在个别的时候吗?
提前致谢!
解决方案
首先,您的错误是因为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
推荐阅读
- html - 以反应形式更改第 i 个输入字段 ondelete 按钮的布尔值
- ios - 如何使用设备支持的文件从 Xcode 10.2.1 在 iOS 13 设备中运行/安装应用程序
- javascript - Sequelize 中的关联未按预期工作
- react-native - 世博会 - 将方向锁定为横向
- html - Yelp Fusion API JSON 评论解析
- reactjs - Google-maps-react 未加载地图
- wordpress - Wordpress 如何为公众生成修订列表(帖子和页面)
- r - 如何根据多个条件连接两个数据框?
- c# - 当不包含在 Formatters 命名空间中时,我如何使用协议 Soap?
- c++ - 访问嵌套结构的数据成员并将结构传递给函数 C++