I find that I am consistently trying to determine the first and last day of the current month using Transact SQL. The following is a cheat sheet for calculating easily those dates:

DECLARE@todayDATETIME,@first_of_month DATETIME,@last_of_month DATETIME

 

SET@today=GETDATE()

SET@first_of_month=DATEADD(dd,-(DATEPART(dd, @today)-1),@today)

SET@last_of_month=DATEADD(dd,–1,DATEADD(mm, 1, @first_of_month))

 

PRINT@today

PRINT@first_of_month

PRINT @last_of_month

Advertisements