DATENAME returns the name or numeric value for the specified date component.

Syntax

DATENAME( dateType, date )

Arguments

  • dateType: the date type. The allowed values are: year, month, day, hour, minute, second, dayofyear, dayofweek, quarter, week. This value is case insensitive.
  • date: the date value. This value should be of type DATE, TIME, or TIMESTAMP.

Returns

  • The value for the specified dateType.

  • This value may be a string name or an INTEGER. For options ‘month’ and ‘dayofweek’, it returns their names.

Examples

SELECT DATENAME(Year,'2013-07-23 23:59:59'), DATENAME(Month,'2013-07-23 23:59:59'),
      DATENAME(day, '2013-07-23 23:59:59'), DATENAME(hour, '2013-07-23 23:59:59'),
      DATENAME(minute,'2013-07-23 23:59:59'), DATENAME(second,'2013-07-23 23:59:59'),
      DATENAME(dayofyear, '2013-07-23 23:59:59'), DATENAME(dayofweek, '2013-07-23 23:59:59') ,
      DATENAME(week, '2013-07-23 23:59:59'), DATENAME(quarter, '2013-07-23 23:59:59')
FROM "redshift1.inventory.sampleData";

-- Returns
2013  July        3     23    59    59    204   Tuesday     30    3