星期日, 2月 01, 2009

SQL 指令 查詢時間區段

1、SQL指令 BETWEEN 是存在於範圍內時,為true


建立一個資料表格式如下:

String
SmallDateTime
String
WID RecTime
Name
6 2008/09/01 PM 01:00:00 John
7 2008/10/21 PM 01:00:00 John
8
2009/01/01 PM 01:00:00 John


(1)、若是下sql指令:

Select WID From Person Where WID BETWEEN 6 AND 8


那資料庫會回傳
WID
6
7
8

(2)、如果想查詢 2008年整年的資料
Select RecTime From Person Where RecTime BETWEEN '2008-01-01 00:00:00' AND '2008-12-31 23:59:59'

若只是寫 2008-01-01 AND 2008-12-31 ,那後面的時間應該只會落在 2008-12-31 00:00:00,如此一來便不會查到 2008-12-31這一天時間內的資料了。



2、另一種方式

Select * From Event Where Year(Create_Time)='2008'

Select * From Event Where Year(Create_Time)='2008' AND Month(Create_Time)='7'

沒有留言:

張貼留言