我有兩個日期,從日期和到日期。如何選擇使用where子句?
另外我有兩個時間字段,從時間和到時間。
數據庫中的日期字段是Datetime
。我需要根據日期和時間選擇數據。
這是我選擇的數據13:00 至15:00查詢,但它不適合20:00至08:00。
where Date>= '2/01/2012' AND Date<'2/28/2013'
AND CAST(Date AS TIME) BETWEEN '20:00' AND '08:00'
我有兩個日期,從日期和到日期。如何選擇使用where子句?
另外我有兩個時間字段,從時間和到時間。
數據庫中的日期字段是Datetime
。我需要根據日期和時間選擇數據。
這是我選擇的數據13:00 至15:00查詢,但它不適合20:00至08:00。
where Date>= '2/01/2012' AND Date<'2/28/2013'
AND CAST(Date AS TIME) BETWEEN '20:00' AND '08:00'
您需要將「日期」和「時間」部分組合在一起。
此代碼將說明如何做到這一點:
SELECT the_date
, the_time
, DateAdd(hh, DatePart(hh, the_time), the_date) As hour_added
, DateAdd(mi, DatePart(mi, the_time), the_date) As minute_added
, DateAdd(mi, DatePart(mi, the_time), DateAdd(hh, DatePart(hh, the_time), the_date)) As both_added
FROM (
SELECT Cast('2013-02-28' As datetime) As the_date
, Cast('08:30' As datetime) As the_time
) As example
然後可以使用所得到的值在比較
你必須拿出兩個日期時間值做得到正確的邏輯。爲此,您可能必須先將日期和時間值轉換爲字符串,然後將字符串轉換爲日期時間。比我聰明的人可能會知道如何直接做到這一點。 –
您是否只想返回輸入間隔完全在行間隔中的行?或者也部分 – Hazaart
請檢查我的更新回答 – htxryan