AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

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

By pushpam abhishek
Listen to this article

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 this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments