In this post explains how to use the DATENAME function in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), the DATENAME function returns a specified part of a given date, as a string value.
The syntax for the DATENAME function in SQL Server (Transact-SQL) is:
DATENAME( interval, date )
The time/date interval that you wish to retrieve from date. It can be one of the following values:
Value (any one of) | Explanation |
---|---|
year, yyyy, yy | Year interval |
quarter, qq, q | Quarter interval |
month, mm, m | Month interval |
dayofyear | Day of year interval |
day, dy, y | Day interval |
week, ww, wk | Week interval |
weekday, dw, w | Weekday interval |
hour, hh | Hour interval |
minute, mi, n | Minute interval |
second, ss, s | Second interval |
millisecond, ms | Millisecond interval |
The date to use to retrieve the interval value.
The DATENAME function can be used in the following versions of SQL Server (Transact-SQL):
Let's look at some SQL Server DATENAME function examples and explore how to use the DATENAME function in SQL Server (Transact-SQL).
For example:
SELECT DATENAME(year, '2014/04/28');
Output: '2014'
SELECT DATENAME(yyyy, '2014/04/28');
Output: '2014'
SELECT DATENAME(yy, '2014/04/28');
Output: '2014'
SELECT DATENAME(month, '2014/04/28');
Output: 'April'
SELECT DATENAME(day, '2014/04/28');
Output: '28'
SELECT DATENAME(quarter, '2014/04/28');
Output: '2'
SELECT DATENAME(hour, '2014/04/28 09:49');
Output: '9'
SELECT DATENAME(minute, '2014/04/28 09:49');
Output: '49'
SELECT DATENAME(second, '2014/04/28 09:49:12');
Output: '12'
SELECT DATENAME(millisecond, '2014/04/28 09:49:12.726');
Output: '726'