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 INTSELECT
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'
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 NVARCHARSELECT 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
Post A Comment:
0 comments: