日期:2014-05-17  浏览次数:20660 次

怎样快速,1个月有多少天? --有挑战!
需求通过月份,得到整个月份天数列表(以TABLE形式显示)

表1
COL_NAME COL_MONTH   
AAA      2013-02
BBB      2013-03
------------------------------
期望得到结果
COL_NAME COL_MONTH   
AAA      2013-02-01
.........此处省略
AAA      2013-02-28
BBB      2013-03-01
..........此处省略
BBB      2013-03-31

------解决方案--------------------


declare @i int,@sdt varchar(10),@edt varchar(10)
set @i=1
set @sdt=datename(yy,getdate())+'-'+cast(@i as varchar)+'-01'
set @edt=convert(varchar,dateadd(m,1,@sdt),23)
--年限你自己控制一下吧
select @sdt,@edt

;with cte as(
select dateadd(day,number,@sdt) time
from master..spt_values where type='P' and number<31
)select * from cte where time<@edt

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