据我所知,不需要 Date(...) 。这个例子似乎有效
DECLARE @TheDate Date = '2012-07-01';
SELECT 'hello' WHERE (@TheDate BETWEEN '2012-04-01' AND '2012-06-30')
--None returned
SET @TheDate = '2012-05-01'
SELECT 'hello' WHERE (@TheDate BETWEEN '2012-04-01' AND '2012-06-30')
--selects hello
顺便说一句,值得用日期时间答案查看这个问题(将在此处发布以节省精力)
between 语句可能导致日期范围边界问题
BETWEEN '01/01/2009' AND '01/31/2009'
真的被解释为
BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 00:00:00'
所以会错过 1 月 31 日当天发生的任何事情。在这种情况下,您将不得不使用:
myDate >= '01/01/2009 00:00:00' AND myDate < '02/01/2009 00:00:00' --CORRECT!
或者
BETWEEN '01/01/2009 00:00:00' AND '01/31/2009 23:59:59' --WRONG! (see update!)
更新:完全有可能在一天的最后一秒内创建记录,日期时间最晚为 01/01/2009 23:59:59.997!
因此,BETWEEN (firstday) AND (lastday 23:59:59)
不建议使用该方法。
请改用该myDate >= (firstday) AND myDate < (Lastday+1)
方法。