A general date/time function that returns a CHARACTER STRING containing the name of the value of a specified part of a date/time expression.
Synopsis
DATENAME(datepart,date-expression)
Arguments
datepart  The type of date/time information to return. 
date-expression  A date/time expression from which the date part is to be returned. date-expression must contain a value of type datepart
Description
The DATENAME function returns the name of the specified part (such as the month "June") of a date/time value. The result is returned as a CHARACTER STRING. If the result is numeric (such as "23" for the day), it is still returned as a CHARACTER STRING. To return this information as an integer, use DATEPART. To return a string containing multiple date parts, use TO_DATE.
The datepart parameter can be a string containing one (and only one) of the following date/time components, either the full name (the Date Part column) or its abbreviation (the Abbreviation column). These component names and abbreviations are not case-sensitive.
Date Part Abbreviation Return Values
year  yy  1840-9999 
quarter  qq  1-4 
month  mm  January,...December 
week  wk  1-53 
weekday  dw  Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday 
dayofyear  dy  1-366 
day  dd  1-31 
hour  hh  0-23 
minute  mi  0-59 
second  ss  0-59 
millisecond  ms  0-99 (with precision of 2) 
sqltimestamp  sts  SQL_TIMESTAMP: yy-mm-dd hh:mm:ss 
The preceding table shows the default return values for the various date parts. You can modify the returned values for several of these date parts by using the SET OPTION command with various time and date options.
Note that DATENAME is provided for Sybase and Microsoft SQL Server compatibility.
Date Expression Formats
The date-expression parameter can be in any of the following formats:
The Caché %String (or compatible) value can be in any of the following formats:
Sybase/SQL-Server-date is one of these five formats:
mmdelimiterdddelimiter[yy]yy
dd Mmm[mm][,][yy]yy
dd [yy]yy Mmm[mm]
yyyy Mmm[mm] dd
yyyy [dd] Mmm[mm]
where delimiter is a slash (/), hyphen (-), or period (.).
Sybase/SQL-Server-time represents one of these three formats:
HH:MM[:SS:SSS][{AM|PM}]
HH:MM[:SS.S]
HH['']{AM|PM}
If the year is given as two digits, the %DATE utility's sliding-date window is used (if applicable) to determine the century.
Range and Value Checking
DATENAME performs the following checks on input values. If a value fails a check, the null string is returned.
Examples
The following examples all return 'Wednesday' because that is the day of week ('dw') of the specified date:
SELECT DISTINCT DATENAME('dw','2004-02-25') AS DayName
FROM Sample.Person
SELECT DISTINCT DATENAME('dw','02/25/2004') AS DayName
FROM Sample.Person
SELECT DISTINCT DATENAME('dw',59590) AS DayName
FROM Sample.Person
The following example returns 'December' because that is the month name ('mm') of the specified date:
SELECT DISTINCT DATENAME('mm','1999-12-20 12:00:00') AS MonthName
FROM Sample.Person
The following example returns '1999' (as a string) because that is the year ('yy') of the specified date:
SELECT DISTINCT DATENAME('yy','1999-12-20 12:00:00') AS Year
FROM Sample.Person
Note that the above examples use the abbreviations of the date parts. However, you can specify the full name, as in this example:
SELECT DISTINCT DATENAME('year','1999-12-20 12:00:00') AS Year
FROM Sample.Person
The following example returns the full SQL timestamp:
SELECT DISTINCT DATENAME('sts','1999-12-20') AS TStamp
FROM Sample.Person
In this case, DATENAME fills in the missing time information to return a timestamp string of '1999–12–20 00:00:00'.
See Also