Technical Article

Finding first/last day of month by a date

,

A better version of the :
"Finding first/last day of month by a date"

DECLARE @SomeDate DATETIME
SELECT@SomeDate = GETDATE()
SELECTCONVERT(datetime, CONVERT(VARCHAR(6), DATEADD(MONTH, -1, @SomeDate), 112) + '01', 112) as FirstDayPrevMonth,
CONVERT(datetime, CONVERT(VARCHAR(6), @SomeDate, 112) + '01', 112) as FirstDayThisMonth, 
CONVERT(datetime, CONVERT(VARCHAR(6), DATEADD(MONTH, +1, @SomeDate), 112) + '01', 112) as FirstDayNextMonth,

CONVERT(datetime, CONVERT(VARCHAR(6), @SomeDate, 112) + '01', 112) - 1 as LastDayPrevMonth, 
CONVERT(datetime, CONVERT(VARCHAR(6), DATEADD(MONTH, +1, @SomeDate), 112) + '01', 112) - 1 as LastDayThisMonth, 
CONVERT(datetime, CONVERT(VARCHAR(6), DATEADD(MONTH, +2, @SomeDate), 112) + '01', 112) - 1 as LastDayNextMonth

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating