日期:2014-05-18  浏览次数:20466 次

sql得到时间
查询2012-06-07到2012-07-05这段时间内所有的日期
希望的查询结果为:
2012-06-07
2012-06-08
2012-06-09
...
2012-07-03
2012-07-04
2012-07-05

------解决方案--------------------
between '2012-06-07' and '2012-07-05'
------解决方案--------------------
SQL code

select * from tab
where 日期 between '2012-06-07' and '2012-07-05'

------解决方案--------------------
SQL code

declare @st datetime = '2012-06-07'
declare @et datetime = '2012-07-05'
declare @t datetime 
set @t = @st 
while(@t<=@et)
begin

    print convert(varchar(10),@t,120)
    set @t = dateadd(day,1,@t)
end

------解决方案--------------------
SQL code
select convert(varchar(10),dateadd(dd,number-1,'2012-06-07'),120) as dt
from master..spt_values
where type='P' and dateadd(dd,number-1,'2012-06-07')<='2012-07-05'
/**
dt
----------
2012-06-06
2012-06-07
2012-06-08
2012-06-09
2012-06-10
2012-06-11
2012-06-12
2012-06-13
2012-06-14
2012-06-15
2012-06-16
2012-06-17
2012-06-18
2012-06-19
2012-06-20
2012-06-21
2012-06-22
2012-06-23
2012-06-24
2012-06-25
2012-06-26
2012-06-27
2012-06-28
2012-06-29
2012-06-30
2012-07-01
2012-07-02
2012-07-03
2012-07-04
2012-07-05

(30 行受影响)
**/

------解决方案--------------------
http://topic.csdn.net/u/20090906/17/a91cd31e-abf7-4190-8486-c5491aa63c71.html?seed=1380177511&r=59598572#r_59598572