SQL Server: DATENAME Function

DATENAME function

This SQL Server tutorial explains how to use the DATENAME function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the DATENAME function returns a specified part of a given date, as a string value.

Syntax

The syntax for the DATENAME function in SQL Server (Transact-SQL) is:

DATENAME( interval, date )

Parameters or Arguments

interval

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, yyYear interval
quarter, qq, qQuarter interval
month, mm, mMonth interval
dayofyearDay of year interval
day, dy, yDay interval
week, ww, wkWeek interval
weekday, dw, wWeekday interval
hour, hhHour interval
minute, mi, nMinute interval
second, ss, sSecond interval
millisecond, msMillisecond interval

dateThe date to use to retrieve the interval value.

Note

  • The DATENAME function returns the result as a string value.
  • The DATEPART function returns the result as an integer value.

Applies To

The DATENAME functions can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let’s look at some SQL Server DATENAME functions examples and explore how to use the DATENAME functions in SQL Server (Transact-SQL).

For example:

SELECT DATENAME(year, '2014/04/28');
Result: '2014'

SELECT DATENAME(yyyy, '2014/04/28');
Result: '2014'

SELECT DATENAME(yy, '2014/04/28');
Result: '2014'

SELECT DATENAME(month, '2014/04/28');
Result: 'April'

SELECT DATENAME(day, '2014/04/28');
Result: '28'

SELECT DATENAME(quarter, '2014/04/28');
Result: '2'

SELECT DATENAME(hour, '2014/04/28 09:49');
Result: '9'

SELECT DATENAME(minute, '2014/04/28 09:49');
Result: '49'

SELECT DATENAME(second, '2014/04/28 09:49:12');
Result: '12'

SELECT DATENAME(millisecond, '2014/04/28 09:49:12.726');
Result: '726'

Leave a Reply