首页 > 解决方案 > where 子句中的 case 语句使用 not like and is not null

问题描述

我正在使用存储过程从现有表中提取数据,该存储过程具有用户通过复选框在前端选择的一些是或否选项。我想限制为他们做出的每一个选择写一堆不同的If陈述。

我的 where 子句的这一部分有效。此列的数据是YN

Where... and IsSigned = Case When @IncludeSigned = 'Y' then IsSigned else 'N' end

我想添加到 where usingis not null以及not like方括号之间是否可能。到目前为止我有

and SignatureType = case when @IncludeElectronic = 'Y' then Type else [NOT like electronic] end

and ReviewDate = Case When @HasReviewDate = 'Y' then [ReviewDate is not null] else null end

标签: sqlsql-serverwhere

解决方案


这可能会帮助您使用 AND/OR 而不是大小写

where (ReviewDate is not null or @HasReviewDate = 'Y' ) And (....)

即当@HasReviewDate = 'Y' 查询将返回ReviewDate 不为null 的记录,当@HasReviewDate != 'Y' 然后查询将返回ReviewDate 为null 的记录


推荐阅读