MS SQL得到指定日期的当月月末

MS SQL得到指定日期的当月月末

declare @ddate date
select @ddate=dateadd(day,-1,dateadd(month,1+datediff(month,0,'2016-01-21'),0))
select @ddate
--2016-01-31

declare @ddatetime datetime
select @ddatetime=dateadd(second,59,dateadd(minute,59,dateadd(hour,23,dateadd(month,1+datediff(month,0,'2016-01-21'),0)-1)))
select @ddatetime
--2016-01-31 23:59:59.000

 输入一个日期,每天执行一次,执行到当月月末

declare @ddate date,@enddate date
set @ddate='2015-12-01'
set @enddate=dateadd(day,-1,dateadd(month,1+datediff(month,0,@ddate),0))--dateadd(month,1,@ddate)
while @ddate<=@enddate
begin
    print cast(@ddate as varchar(10))+':已完成'    
    set @ddate=dateadd(day,1,@ddate)
end

 

你可能感兴趣的:(MS SQL得到指定日期的当月月末)