MS SQL Tips & Tricks

How to find a number of days in a year

The easiest way to find the number of days in a year, will be finding its first and last day date and just get the diffrence between them. As bellow:

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

select datediff(d,
	dateadd( year, 
		datediff( year, 0, @cDateTime ), 
		0 ),
		 
		dateadd(d,-1,dateadd( year, 
			datediff( year, 0, @cDateTime )+1,0 
		))
	)+1 as DaysInYear

Learn more tricks

Add Comment

Comments