ms sql 2008查询...和 [英] ms sql 2008 query for between...and
问题描述
你好专家,
i写了两个日期之间所有记录的查询..
但问题是我得到了所有记录,除了结束(第二)日期
例子---
i想要22-08-2012和5-09-2012之间的所有记录
i从22-8-2012开始记录到4-09-2012
所以请告诉我如何获取5-09-2012的数据??????
提前感谢...
hello experts,
i wrote a query for getting all records between two dates..
but the problem is that i got all records except end(second) date
example---
i wants all records between 22-08-2012 and 5-09-2012
i got records starting from 22-8-2012 to 4-09-2012
so please tell me what to do for getting data of 5-09-2012??????
thanks in advance...
推荐答案
您可能在日期时间格式的日期列中存储了值
看到差异......
May be you have stored value in your date column in datetime format
see the difference...
select case when '2013-02-07' between '2013-02-05' and '2013-02-07' then 'found' else 'not found' end
--result : found
select case when '2013-02-07 12:9:9 am' between '2013-02-05' and '2013-02-07' then 'found' else 'not found' end
--result : not found
select case when convert(datetime,convert(varchar(10),'2013-02-07 12:9:9 am',103)) between '2013-02-05' and '2013-02-07' then 'found' else 'not found' end
--result : found
所以,你应该像我在第三次查询中那样将 日期时间值转换为日期 ...
快乐编码!
:)
so, you should convert datetime value to date like I do in 3rd query...
Happy Coding!
:)
这里有一篇文章给你:
如何在两个日期之间进行搜索并获取所有日期记录 [ ^ ]
here is an article for you:
How to search between two dates and get all records[^]
hi,
hi ,
AND (CREATEDDATE between @FROMDATE and @TODATE)
AND ( CREATEDDATE <= @TODATE OR CREATEDDATE >= @FROMDATE)
请尝试一次
please try once
这篇关于ms sql 2008查询...和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!