SQL Server – How to get First and Last day of current and previous month without time

Get first day of current month
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0) AS FirstDayOfCurrentMonth

Get last day of current month
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) +1, -1) AS LastDayOfCurrentMonth

Get first day of previous month
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE())-1, 0) AS FirstDayOfPreviousMonth

Get last day of previous month
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), -1) AS LastDayOfPreviousMonth

Get first day of month last year
SELECT DATEADD(yyyy, -1, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0)) AS FirstDayOfMonthLastYear

Get last day of month last year
SELECT DATEADD(yyyy, -1, DATEADD(mm, DATEDIFF(mm, -1, GETDATE()), -1)) AS LastDayOfMonthLastYear

Leave a Reply

Your email address will not be published. Required fields are marked *