Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

DATENAME

Example

Retrieve a particular component of a date

SELECT DATENAME(year, ‘2017/08/25’AS DatePartString;

Definition and Usage

The DATENAME() function retrieves a specified part of a date and returns the result as a string value.

Syntax

DATENAME(intervaldate)

Parameter Values

Parameter

Description

interval

Required: Specify the part of the date to retrieve. Options include:

  • year, yyyy, yy: Year
  • quarter, qq, q: Quarter
  • month, mm, m: Month
  • dayofyear: Day of the year
  • day, dy, y: Day
  • week, ww, wk: Week
  • weekday, dw, w: Weekday
  • hour, hh: Hour
  • minute, mi, n: Minute
  • second, ss, s: Second
  • millisecond, ms: Millisecond

 

Date

Required: Specifies a month ranging from 1 to 12.

Technical Details

 

Return type:

nvarchar

Works in:

SQL Server (beginning from 2008), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse

 

More Examples

Example

Retrieve and return a specific component of a date.

SELECT DATENAME(yy, ‘2017/08/25’AS DatePartString;

Example

Retrieve and provide a specific segment of a date.

SELECT DATENAME(month, ‘2017/08/25’AS DatePartString;

Example

Retrieve and provide a specific segment of a date.

SELECT DATENAME(hour, ‘2017/08/25 08:36’AS DatePartString;

Example

Retrieve a specific segment of a date

SELECT DATENAME(minute, ‘2017/08/25 08:36’AS DatePartString;