Get first & last day of the month
I’m building a Stored Procedure and Report that sets the default start date as the first day of the month one (1) month ago. Can easily be adapted to this month, twelve (12) months ago, etc.
Can be achieved like so:
-- First day of prev month
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
-- Lastday of prev month
SELECT DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(-1)
=DateSerial(Year(Now()), Month(Now()), "1").AddDays(-1)
Leave a comment