Get first & last day of the month

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)
Mike250

I'm an Australian Chief Analytics Officer passionate about data science, visual insights, and all things sportโ€”particularly cricket. An adventurer at heart, Iโ€™ve gone from abseiling cliffs to snorkeling in crystal-clear waters, sleeping in the wilds of Africa, and exploring destinations worldwide, with my latest trip taking me to Bali. When I'm not diving into data or analytics, I'm spending time with my three sons and two daughters, attempting to hit sixes for my local cricket club, reviewing chicken schnitzels or honing my craft around a coffee machine.

Related Posts
Leave a comment

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