递归方式生成指定月份所有日期

DECLARE @SELECTMONTH NVARCHAR(7)=N'2017-02';
;
WITH MONTHDAYTABLE([DATE]) AS(
SELECT CONVERT(DATETIME,@SELECTMONTH+'-01')
UNION ALL
SELECT DATEADD(D,1,MONTHDAYTABLE.[DATE]) AS DATE
FROM MONTHDAYTABLE
WHERE DATEADD(D,1,MONTHDAYTABLE.[DATE])<DATEADD(MONTH,1,@SELECTMONTH+'-01')
)

SELECT * FROM MonthDayTable

posted @ 2017-06-14 09:33  青衫  阅读(198)  评论(0编辑  收藏  举报