MS SQL Tips & Tricks

How to find first and last day of the year

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

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

select dateadd( year, 
		datediff( year, 0, @cDateTime ), 
		0 ) as BeginOfTheYear;
		 
select 	dateadd(d,-1,dateadd( year, 
			datediff( year, 0, @cDateTime )+1,0 
		)) as EndOfTheYear

Learn more tricks

Add Comment

Comments