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 |
| ||||||||||||||||||||||||||||||||
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:
OrderQuantity | Price | OrderTime |
---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 |
120 | 1.61 | 2018-03-23 07:14:16 |
125 | 1.78 | 2018-09-12 05:25:56 |
50 | 1.80 | 2019-01-16 11:52:05 |
200 | 1.72 | 2020-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:
OrderQuantity | Price | OrderTime | DATENAME_Value |
---|---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 | August |
120 | 1.61 | 2018-03-23 07:14:16 | March |
125 | 1.78 | 2018-09-12 05:25:56 | September |
50 | 1.80 | 2019-01-16 11:52:05 | January |
200 | 1.72 | 2020-02-06 09:31:34 | February |
❮ T-SQL Functions