需要在sqlserver中查询以下条件

时间:2015-09-02 10:08:34

标签: sql sql-server

我需要根据下面给出的条件优化以下现有查询:

现有查询:

Select * from table where 

(
        @payEffectiveDate > payEffectiveDate 
        AND payEffectiveDate    < payCancelDate 
)

在上面的where子句中提到的查询条件只有在where子句满足条件时才应该执行。我的意思是在where子句中我们需要检查以下条件,如果它满足那么只需要满足上述条件。

@EffectiveDate >    payEffectiveDate  
        AND @EffectiveDate  < payCancelDate

1 个答案:

答案 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)