我四处搜索,但没有找到与我要实现的目标类似的情况。我只想在条件为真时才执行where子句表达式,否则我想忽略不匹配的数据。
Where (
case when LeaveDateTime > FromDate and EnteringDateTime < ToDate then
(dateadd(d, Threshhold, LeaveDateTime) >= EnteringDateTime
end
);
我在 '>=' 附近得到不正确的语法 有人可以帮我解决这个问题吗?
最佳答案
用简单的逻辑替换它:
Where (not (LeaveDateTime > FromDate and EnteringDateTime < ToDate) or
dateadd(day, Threshhold, LeaveDateTime) >= EnteringDateTime
);
如果变量取NULL
值,那么逻辑就有些繁琐了。
您的版本不起作用,因为 case
是一个标量表达式。 bool 比较不是标量表达式,因此它不能是then
返回的值。
关于sql - 条件 where 子句 SQL Server 2012,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27851194/