2017-03-01 74 views
0

我需要根據日期和時間選擇數據。我有兩個標準。我怎樣才能實現這個?用時間線過濾sql的結果

  1. 選擇隨時間的憤怒 10:00 2013年1月1日和2013年1月10日之間的數據到16:00
  2. 2013年1月1日之間選擇數據1-10- 2013與 時間範圍在第二天早上20:00至08:00

我實現了一個代碼。它只適用於第一個標準。代碼如下:

where date >= '1-1-2013' 
and date < '1-10-2013' 
and cast(date as time) between '10:00' and '16:00' 

這裏表中的日期字段是datetime類型。請幫忙解決這個問題。

+0

哪個SQL的執行是您使用? – WilliamD

+0

另外,你不清楚你期待什麼輸出。該查詢是否應返回包含匹配兩個條件的記錄的單個結果集? – WilliamD

+0

對不起,我沒有得到你。 – Mansoor

回答

1

試試這個用一堆的OR:

select * 
from your_table 
where date >= '2013-01-01' 
    and date < '2013-01-11' 
    and (
     cast(date as time) between '10:00' and '16:00' 
     or cast(date as time) >= '20:00' 
     or cast(date as time) <= '08:00' 
     ); 
+0

謝謝!有效!!! – Mansoor

0
select * from your_table 
    where date >='2013-01-01' and date <= '2013-01-11' 
    and convert(varchar,date,8)>'10:00' and convert(varchar,date,8)<'16:00'