MS SQL Tips & Tricks

How to find first and last day of the month

The sample bellow shows how to find the start and end date of the month containing specified date.

declare @cDateTime datetime;
set @cDateTime = GetDate()

select dateadd( month, 
		datediff( month, 0, @cDateTime ), 
		0 ) as BeginOfTheMonth;
		 
select 	dateadd(d,-1,dateadd( month, 
			datediff( month, 0, @cDateTime )+1,0 
		)) as EndOfTheMonth

Learn more tricks

Add Comment

Comments