MS SQL 日期 Between

时间:2021-08-10 03:10:36

摘要: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‘

一般应该比较常使用上述的语法吧!

有错误或更的好的方法,欢迎回复,非常感谢!

原文:大专栏  MS SQL 日期 Between