Simpler First/Last Day of Months

,

Script uses simpler Dateadd function ensuring additions/substractions are based from 1st of month.

declare @TruncDate datetime
set @SomeDate = getdate()
set @TruncDate=Cast(convert(varchar(11),@SomeDate,113) as DateTime)
select 
Dateadd(Month,-1,Dateadd(Day,-Datepart(day,@TruncDate)+1,@TruncDate)) as FirstDayPrevMonth,
Dateadd(Day,-Datepart(day,@TruncDate)+1,@TruncDate) as FirstDayThisMonth,
Dateadd(Month,1,Dateadd(Day,-Datepart(day,@TruncDate)+1,@TruncDate)) as FirstDayNextMonth,
Dateadd(Day,-Datepart(day,@TruncDate),@TruncDate) as LastDayPrevMonth,
Dateadd(Day,-1,Dateadd(Month,1,Dateadd(Day,-Datepart(day,@TruncDate)+1,@TruncDate))) as LastDayThisMonth,
Dateadd(Day,-1,Dateadd(Month,2,Dateadd(Day,-Datepart(day,@TruncDate)+1,@TruncDate))) as LastDayNextMonth

Rate

Share

Share

Rate