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

怎样用SQL得到一年的每一天?
怎样用SQL得到一年的每一天?


例如:表中总共有三列

字符串日期格式(varchar(50)) 时间日期格式(datetime) 备注(varchar(100))

2012年1月1日 星期日 2012-1-1 null  

2012年1月2日 星期一 2012-1-2 null


...................

2012年12月31日 星期一

--------------------------------------------

以此类推,一直到年底的最后一天

------解决方案--------------------
生成两个时间之间的所有日期
------解决方案--------------------
SQL code
select dateadd(dd,number,'2012-1-1') AS date 
    from master..spt_values 
    where type='p' and dateadd(dd,number,'2012-1-1')<='2012-12-30'


date
-----------------------
2012-01-01 00:00:00.000
2012-01-02 00:00:00.000
2012-01-03 00:00:00.000
2012-01-04 00:00:00.000
2012-01-05 00:00:00.000
2012-01-06 00:00:00.000
2012-01-07 00:00:00.000
2012-01-08 00:00:00.000
2012-01-09 00:00:00.000
2012-01-10 00:00:00.000
2012-01-11 00:00:00.000
2012-01-12 00:00:00.000
2012-01-13 00:00:00.000
2012-01-14 00:00:00.000
2012-01-15 00:00:00.000
2012-01-16 00:00:00.000
2012-01-17 00:00:00.000
2012-01-18 00:00:00.000
2012-01-19 00:00:00.000
2012-01-20 00:00:00.000
2012-01-21 00:00:00.000
2012-01-22 00:00:00.000
2012-01-23 00:00:00.000
2012-01-24 00:00:00.000
2012-01-25 00:00:00.000
2012-01-26 00:00:00.000
2012-01-27 00:00:00.000
2012-01-28 00:00:00.000
2012-01-29 00:00:00.000
2012-01-30 00:00:00.000
2012-01-31 00:00:00.000
2012-02-01 00:00:00.000
2012-02-02 00:00:00.000
2012-02-03 00:00:00.000
2012-02-04 00:00:00.000
2012-02-05 00:00:00.000
2012-02-06 00:00:00.000
2012-02-07 00:00:00.000
2012-02-08 00:00:00.000
2012-02-09 00:00:00.000
2012-02-10 00:00:00.000
2012-02-11 00:00:00.000
2012-02-12 00:00:00.000
2012-02-13 00:00:00.000
2012-02-14 00:00:00.000
2012-02-15 00:00:00.000
2012-02-16 00:00:00.000
2012-02-17 00:00:00.000
2012-02-18 00:00:00.000
2012-02-19 00:00:00.000
2012-02-20 00:00:00.000
2012-02-21 00:00:00.000
2012-02-22 00:00:00.000
2012-02-23 00:00:00.000
2012-02-24 00:00:00.000
2012-02-25 00:00:00.000
2012-02-26 00:00:00.000
2012-02-27 00:00:00.000
2012-02-28 00:00:00.000
2012-02-29 00:00:00.000
2012-03-01 00:00:00.000
2012-03-02 00:00:00.000
2012-03-03 00:00:00.000
2012-03-04 00:00:00.000
2012-03-05 00:00:00.000
2012-03-06 00:00:00.000
2012-03-07 00:00:00.000
2012-03-08 00:00:00.000
2012-03-09 00:00:00.000
2012-03-10 00:00:00.000
2012-03-11 00:00:00.000
2012-03-12 00:00:00.000
2012-03-13 00:00:00.000
2012-03-14 00:00:00.000
2012-03-15 00:00:00.000
2012-03-16 00:00:00.000
2012-03-17 00:00:00.000
2012-03-18 00:00:00.000
2012-03-19 00:00:00.000
2012-03-20 00:00:00.000
2012-03-21 00:00:00.000
2012-03-22 00:00:00.000
2012-03-23 00:00:00.000
2012-03-24 00:00:00.000
2012-03-25 00:00:00.000
2012-03-26 00:00:00.000
2012-03-27 00:00:00.000
2012-03-28 00:00:00.000
2012-03-29 00:00:00.000
2012-03-30 00:00:00.000
2012-03-31 00:00:00.000
2012-04-01 00:00:00.000
2012-04-02 00:00:00.000
2012-04-03 00:00:00.000
2012-04-04 00:00:00.000
2012-04-05 00:00:00.000
2012-04-06 00:00:00.000
2012-04-07 00:00:00.000
2012-04-08 00:00:00.000
2012-04-09 00:00:00.000
2012-04-10 00:00:00.000
2012-04-11 00:00:00.000
2012-04-12 00:00:00.000
2012-04-13 00:00:00.000
2012-04-14 00:00:00.000
2012-04-15 00:00:00.000
2012-04-16 00:00:00.000
2012-04-17 00:00:00.000
2012-04-18 00:00:00.000
2012-04-19 00:00:00.000
2012-04-20 00:00:00.000
2012-04-21 00:00:00.000
2012-04-22 00:00:00.000
2012-04-23 00:00:00.000
2012-04-24 00:00:00.000
2012-04-25 00:00:00.000
2012-04-26 00:00:00.000
2012-04-27 00:00:00.000
2012-04-28 00:00:00.000
2012-04-29 00:00:00.000
2012-04-30 00:00:00.000
2012-05-01 00:00:00.000
2012-05-02 00:00:00.000
2012-05-03 00:00:00.000
2012-05-04 00:00:00.000
2012-05-05 00:00:00.000
2012-05-06 00:00:00.000
2012-05-07 00:00:00.000
2012-05-08 00:00:00.000
2012-05-09 00:00:00.000
2012-05-10 00:00:00