我需要根据下面给出的条件优化以下现有查询:
现有查询:
Select * from table where
(
@payEffectiveDate > payEffectiveDate
AND payEffectiveDate < payCancelDate
)
在上面的where子句中提到的查询条件只有在where子句满足条件时才应该执行。我的意思是在where子句中我们需要检查以下条件,如果它满足那么只需要满足上述条件。
@EffectiveDate > payEffectiveDate
AND @EffectiveDate < payCancelDate
答案 0 :(得分:1)
将它们与AND
select *
from Table
where (@EffectiveDate > payEffectiveDate
AND @EffectiveDate < payCancelDate)
AND
(@payEffectiveDate > payEffectiveDate
AND @payEffectiveDate < payCancelDate)
AND
子句确保它周围的条件必须为真。
如果字段的数据类型为DateTime
,那么您也可以这样做
select *
from Table
where (@EffectiveDate between payEffectiveDate AND payCancelDate)
AND (@payEffectiveDate between payEffectiveDate AND payCancelDate)