我有以下函数,它应该返回两个月之间月份的开始和结束日期,但问题是,由于本月是 28 天,因此该函数以 28 天为基础计算所有即将到来的月份,因此返回以下错误值。
StartDate EndDate
-----------------------
2013-02-01 2013-02-28
2013-03-01 2013-03-28
2013-04-01 2013-04-28
declare @sDate datetime,
@eDate datetime;
select @sDate = '2013-02-25',
@eDate = '2013-04-25';
;with months as
(
select DATEADD(mm,DATEDIFF(mm,0,@sDate),0) StartDate,
DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@sDate)+1,0)) EndDate
union all
select dateadd(mm, 1, StartDate),
dateadd(mm, 1, EndDate)
from months
where dateadd(mm, 1, StartDate)<= @eDate
)
select * from months
如何修改它以返回正确的日期?