2017-07-12 34 views
1

嗨我有一個查詢,適用於我的where子句省略日期1/1/2017和3/1/2017。不過,我想使用這個查詢SSRS報告,所以它只是一個@begdate和@enddate,而不是這些硬編碼的字段。有沒有辦法僅僅省略這兩個日期?SQL查詢省略ssrs的某些日期

 where 
((Scheduled_Time >= '01/01/2014 00:00:00' and Scheduled_Time <= '12/31/2016 23:59:59') --filter out 1/1/2017 
    or (Scheduled_Time >= '01/02/2017 00:00:00' and Scheduled_Time <= '02/28/2017 23:59:59') --filter out 3/1/2017 
    or (Scheduled_Time >= '03/2/2017 00:00:00' and Scheduled_Time <= '06/30/2017 23:59:59')) 

回答

1

只是刪除這些日期,然後用你的參數...

where cast(Scheduled_Time as date) <> '20170101' 
     and cast(Scheduled_Time as date) <> '20170301' 
     and Scheduled_Time >= @begdate 
     and Scheduled_Time <= @enddate 
+0

我知道我的想法太辛苦......謝謝。 –

+0

沒問題@TDang。值得注意的是,如果性能成爲一個問題,你應該得到你的第一個方法> =和<=,因爲雖然我的查詢是可選的,但它不應該像你的顯式定義那樣快。這裏是一個例子。 https://dba.stackexchange.com/a/34052/95107,爲了簡潔起見,我只是這樣寫的。 – scsimon