The first day of each month is always 1. However if you interested to find the First and Last Date of each month using a T-Sql Query, here it is:
-- Return First and Last Date of a Month
DECLARE @Month smallint
SET @Month = 1
SELECT DAY(DATEADD(Month, DATEDIFF(Month, -1, getdate()) - 2, -1) + 1) as FirstDayofMonth,
DAY(DATEADD(Month, DATEDIFF(Month,0,getdate())+@Month, -1)) as LastDayOfMonth
If you wish to find out the Lastday of the previous month, just set @Month to 0.
Note: The value for @Month depends on the current month you are in. So if this month is September, the value of @Month is 1. Similarly for Oct(2), Nov(3), Dec(4), Jan(5), Feb(6) and so on.
Also read SQL Server: First and Last Sunday of Each Month
Thanks for your tips...
ReplyDeleteI want to find out First day of each week for weekly report purpose, could you help in finding that?
For February your code is giving 31 days. please check it.
ReplyDeleteHi,
ReplyDeleteThe query works fine. The value for @Month depends on the current month you are in. So if this month is September, the value of @Month is 1. Similarly for Oct(2), Nov(3), Dec(4), Jan(5), Feb(6) and so on..
I have added a note to avoid confusion. Apologies if that wasn't clear earlier.
Thanks for the code, it really helped me. Just what I needed.
ReplyDeleteThank you, this helped me.
ReplyDelete