T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL DATENAME() Function



The T-SQL (Transact-SQL) DATENAME() function returns a specified part of a given date, as a string value.

Syntax

DATENAME(datepart, date)

Parameters

datepart

Required. Specify the unit type to retrieve from the date. It can be one of the following values:

datepartAbbreviations
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
weekdaydw, w
hourhh
minutemi, n
secondss, s
millisecondms
microsecondmcs
nanosecondns
tzoffsettz
iso_weekisowk, isoww
date Required. Specify a date to use to retrieve the datepart value.

Return Value

Returns a string representing the specified datepart of the specified date.

Example 1:

The example below shows the usage of DATENAME() function.

SELECT DATENAME(year, '2019-10-25');
Result: '2019'

SELECT DATENAME(yyyy, '2019-10-25');
Result: '2019'

SELECT DATENAME(yy, '2019-10-25');
Result: '2019'

SELECT DATENAME(quarter, '2019-10-25');
Result: '4'

SELECT DATENAME(month, '2019-10-25');
Result: 'October'

SELECT DATENAME(week, '2019-10-25 08:10:25');
Result: '43'

SELECT DATENAME(day, '2019-10-25');
Result: '25'

SELECT DATENAME(hh, '2019-10-25 08:10:25');
Result: '8'

SELECT DATENAME(hh, '2019-10-25 08:10:25');
Result: '8'

SELECT DATENAME(mi, '2019-10-25 08:10:25');
Result: '10'

SELECT DATENAME(ss, '2019-10-25 08:10:25');
Result: '25'

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42
1201.612018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-02-06 09:31:34

The statement given below can be used to get the month portion of records of column OrderTime:

SELECT *, DATENAME(month, OrderTime) AS DATENAME_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeDATENAME_Value
1001.582017-08-18 10:38:42August
1201.612018-03-23 07:14:16March
1251.782018-09-12 05:25:56September
501.802019-01-16 11:52:05January
2001.722020-02-06 09:31:34February

❮ T-SQL Functions