SQL Server:在两个日期之间提取记录? [英] SQL Server : fetching records between two dates?
本文介绍了SQL Server:在两个日期之间提取记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SQL中,我写了一个 SELECT
语句以在两个日期之间获取数据,使用之间和
例如:
select *
from xxx
where 2012-10-26和2012-10-27之间的日期
但返回的行只有26日,而不是26日和27日。
你能帮助我吗?谢谢。
解决方案
如其他人已经回答,您可能有一个 DATETIME
(或其他变体)列,而不是 DATE
数据类型。
这是一个适用于所有的条件,包括 DATE
:
SELECT *
FROM xxx
WHERE dates> ='20121026'
AND dates< '20121028'---
后一天---转换为'2012-10-28 00:00:00.000'
;
@Aaron Bertrand 已经在以下网址上发表过: BETWEEN
和魔鬼有什么共同点? / p>
In SQL I write a SELECT
statement to fetch data between two dates, using between and
Ex:
select *
from xxx
where dates between '2012-10-26' and '2012-10-27'
But the rows returned are for 26th only, not 26th and 27th.
Can you help me? Thank you.
解决方案
As others have answered, you probably have a DATETIME
(or other variation) column and not a DATE
datatype.
Here's a condition that works for all, including DATE
:
SELECT *
FROM xxx
WHERE dates >= '20121026'
AND dates < '20121028' --- one day after
--- it is converted to '2012-10-28 00:00:00.000'
;
@Aaron Bertrand has blogged about this at: What do BETWEEN
and the devil have in common?
这篇关于SQL Server:在两个日期之间提取记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文