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

生成时间序列2011-01-01到2011-12-31?
生成时间序列2011-01-01到2011-12-31?

2011-01-01
2011-01-02
...
2011-12-31

------解决方案--------------------
SQL code
;with t
as
(select cast('2011-01-01'as datetime) as dt
 union all
 select dt+1 from t
 where dt+1<='2011-12-31')
select * 
from t option(maxrecursion 0)

------解决方案--------------------
SQL code
declare @startdate datetime,@enddate datetime
set @startdate='2011-01-01'
set @enddate='2011-12-31'

select convert(varchar(10),dateadd(day,number,@startdate),120) 
from
    master..spt_values 
where 
    datediff(day,dateadd(day,number,@startdate), @enddate)>=0
    and number>=0 
    and type='p'