SQLServer - DateName Function
Hi,
In this article we will go through the DATENAME() function and how to implement it in SQL Server. Using DATENAME() function you can get any parts of the given date such as Day, Month, Year , Week, Quarter, Weekday, Hour, Minute, Second and so on which would be returned as a string variable.
SQL Server - DATENAME() function
DateName function is used to return the specific date part of the given date.
It will take two input arguments (one should be in the predefined format, the other will be a datetime object) and will return one output which would be in Nvarchar datatype
Syntax:
DATENAME(date-part, Date)
You can get all entities of the ShortDateTime or LongDateTime objecst by using DateName()
Following are some of the list of date parts.
Year
Month
Day
Week
DayofYear
WeekDay
Hour
Minute
Second
Example
Current Date & Time is 29-03-2013 19:10:34.256
Lets consider the following queries for getting the output such as current Year, Monty, Day , Hour, Minute , Second, Week, Quarter and Yearofday from the given current date& Time by using DateName() function.
Query 1:
--------
Select DateName(Year, GetDate()) as Year, DateName(Day,GetDate()) as Today ,DateName(Month,GetDate()) as Month
Here is the resultset of the query
Output
=====
Year Today Month
2013 29 March
Query 2:
--------
SELECT DATENAME(quarter, GETDATE()) as Quarter ,DATENAME(week, GETDATE()) as Week ,DATENAME(dayofyear, GETDATE()) as DayOfYear
Output
=====
Quarter Week DayofYear
1 13 88
Query 2:
--------
SELECT DATENAME(Hour, GETDATE()) as Hour ,DATENAME(Minute, GETDATE()) as Minute,DATENAME(Second, GETDATE()) as Second
Output
=====
Hour Minute Second
19 10 34
Hour will be in 24 hours format