where 子句中的 Case 语句使用 not like and is not null
Case statement in where clause using not like and is not null
我正在使用存储过程从现有 table 中提取数据,该存储过程具有用户通过复选框在前端选择的一些是或否选择。我想限制为他们做出的每个选择编写一堆不同的 If
语句。
我的 where 子句的这一部分有效。此列的数据是 Y
或 N
。
Where... and IsSigned = Case When @IncludeSigned = 'Y' then IsSigned else 'N' end
如果方括号之间可能的话,我想使用 is not null
和 not like
添加到 where 中。到目前为止我有
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
我相信你不能以这种方式使用,除此之外,你的查询中的影响可能会很大,我的建议是改变你正在使用的策略。
这可能会帮助您使用 AND/OR 而不是大小写
where (ReviewDate is not null or @HasReviewDate = 'Y' ) And (....)
即当@HasReviewDate = 'Y' 查询将return ReviewDate 不为空的记录
当@HasReviewDate != 'Y' 时,查询将 return ReviewDate 为空的记录
这样想:-
您的第一个 case 语句有两种可能的结果:-
IsSigned = 'Y'
IsSigned = 'N'
你的后续有问题,因为它们在语法上没有意义。所以第二个写成 returns
SignatureType = Type
SignatureType = [NOT like electronic]
第三个:
ReviewDate = [ReviewDate is not null]
ReviewDate = null end
所以运算符必须在 case 语句之前并应用于 case 语句的所有结果。
例如
WHERE myfield not like CASE WHEN thatfield=1 THEN 'Fish' ELSE 'Chips END
会产生
myfield not like 'Fish'
myfield not like 'Chips'
我正在使用存储过程从现有 table 中提取数据,该存储过程具有用户通过复选框在前端选择的一些是或否选择。我想限制为他们做出的每个选择编写一堆不同的 If
语句。
我的 where 子句的这一部分有效。此列的数据是 Y
或 N
。
Where... and IsSigned = Case When @IncludeSigned = 'Y' then IsSigned else 'N' end
如果方括号之间可能的话,我想使用 is not null
和 not like
添加到 where 中。到目前为止我有
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
我相信你不能以这种方式使用,除此之外,你的查询中的影响可能会很大,我的建议是改变你正在使用的策略。
这可能会帮助您使用 AND/OR 而不是大小写
where (ReviewDate is not null or @HasReviewDate = 'Y' ) And (....)
即当@HasReviewDate = 'Y' 查询将return ReviewDate 不为空的记录 当@HasReviewDate != 'Y' 时,查询将 return ReviewDate 为空的记录
这样想:-
您的第一个 case 语句有两种可能的结果:-
IsSigned = 'Y'
IsSigned = 'N'
你的后续有问题,因为它们在语法上没有意义。所以第二个写成 returns
SignatureType = Type
SignatureType = [NOT like electronic]
第三个:
ReviewDate = [ReviewDate is not null]
ReviewDate = null end
所以运算符必须在 case 语句之前并应用于 case 语句的所有结果。
例如
WHERE myfield not like CASE WHEN thatfield=1 THEN 'Fish' ELSE 'Chips END
会产生
myfield not like 'Fish'
myfield not like 'Chips'