摘要:MS SQL 日期 Between
使用記錄:
MSSQL 資料表(Table)
Key | Name | Date |
20150423001 | Johnny | 2015-04-23 01:53:09.957 |
20150423002 | Mary | 2015-04-23 22:54:33.607 |
SELECT * FROM Table WHERE Date BETWEEN '20150423' AND '20150423'
SELECT * FROM Table WHERE Date BETWEEN CAST('20150423' AS DATETIME) AND CAST('20150423' AS DATETIME)
若使用以上兩種語法皆會查詢無資料,其語法應該修改如下:
SELECT * FROM Table WHERE Date BETWEEN CAST('20150423 00:00:00' AS DATETIME) AND CAST('20150423 23:59:29' AS DATETIME)
主要是加上時間,不然下列語法也可以查詢到資料:
SELECT * FROM Table WHERE Date >= '20150423' AND Date <= '20150423'
一般應該比較常使用上述的語法吧!
有錯誤或更的好的方法,歡迎回覆,非常感謝!