sql

[How] to get Day of Year from date in Sql Server

Using DATENAME Function,How to get Day of Year from Date in SQL Server,Using the DATEPART Function,Day of Year from Date in SQL Server
Share it:

How to get Day of Year from Date in SQL Server

In these articles, we may need to get the Day of Year from Date in SQL Server. In this article, we will see how we can get the Day of Year from Date in SQL Server.

Approach 1: Using the DATEPART Function

We can use the DATEPART() function to get Day of Year from Date in SQL Server, here we need to specify the 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'


Day of Year from date in Sql Server

Approach 2: Using DATENAME Function

We can use the DATENAME() function to get Day of Year from Date in SQL Server, here we need to specify the datepart parameter of the DATENAME function as dayofyear or dy or y all will return the 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
Using DATENAME Function


Share it:

sql

Post A Comment:

0 comments: