WITH r AS ( SELECT dbo.add_months(dbo.substr('201401', 1, 4) + dbo.to_char('2011-01-01 00:00:00.0000000','MMDD'),-1) lv,
dbo.last_day(dbo.add_months(dbo.substr('201401', 1, 4) + dbo.to_char('2011-01-01 00:00:00.0000000','MMDD'),-1)) tv
union all
SELECT dbo.add_months(lv,-1), dbo.last_day(dbo.add_months(tv,-1)) from r
where lv >= dbo.add_months(dbo.substr('201401', 1, 4) + dbo.to_char('2011-01-01 00:00:00.0000000','MMDD'),-11)
) SELECT * FROM r
출력결과
해당달시작일,마지막일
2013-12-01 2013-12-31
2013-11-01 2013-11-30
2013-10-01 2013-10-31
2013-09-01 2013-09-30
2013-08-01 2013-08-31
2013-07-01 2013-07-31
2013-06-01 2013-06-30
2013-05-01 2013-05-31
2013-04-01 2013-04-30
2013-03-01 2013-03-31
2013-02-01 2013-02-28
2013-01-01 2013-01-31
'IT > [DB]MsSql' 카테고리의 다른 글
[mssql] 날짜 재귀로 호출하기 (0) | 2020.01.23 |
---|---|
[MSSQL] OPENXML (0) | 2020.01.17 |
[공통] 날짜 재귀출력 (0) | 2019.11.22 |
날짜 (0) | 2019.11.19 |
CAST and CONVERT (0) | 2019.11.13 |