Tag Archives: Day of Year

How to get Day of Year from date in Sql Server

Many a times we come across a scenario where we may need to get Day of Year from Date in Sql Server. In this article we will see how we can get Day of Year from Date in Sql Server.

Approach 1: Using DATEPART Function

We can use DATEPART() function to get Day of Year from Date in Sql Server, here we need specify datepart parameter of the DATEPART function as dayofyear or dy or y all will return the same result. DATEPART() functions return type is INT

SELECT GETDATE() 'Today', 
                  DATEPART(dayofyear,GETDATE()) 'Day of Year'
SELECT GETDATE() 'Today', DATEPART(dy,GETDATE()) 'Day of Year'
SELECT GETDATE() 'Today', DATEPART(y,GETDATE()) 'Day of Year'

RESULT:
Day of Year using DatePart function in Sql

Approach 2: Using DATENAME Function

We can use DATENAME() function to get Day of Year from Date in Sql Server, here we need specify datepart parameter of the DATENAME function as dayofyear or dy or y all will return the same result. DATENAME() functions return type is NVARCHAR

SELECT GETDATE() 'Today', 
                  DATENAME(dayofyear,GETDATE()) 'Day of Year'
SELECT GETDATE() 'Today', DATENAME (dy,GETDATE()) 'Day of Year'
SELECT GETDATE() 'Today', DATENAME (y,GETDATE()) 'Day of Year'

RESULT:
Day of Year using DATENAME function in Sql

Example: Day of Year from a given date

SELECT '2018-02-21' 'Date', 
                  DATEPART(dayofyear,'2018-02-21') 'Day of Year'
SELECT '2018-02-21' 'Date',DATEPART(dy,'2018-02-21') 'Day of Year'
SELECT '2018-02-21' 'Date',DATEPART(y,'2018-02-21') 'Day of Year'

RESULT:
Example Day of Year using DatePart function in Sql