Caché SQL Reference
A date/time function that returns a string representing the value of the specified part of a date/time expression.
||The type of date/time information to return. The name (or abbreviation) of a date or time part. This name can be specified in uppercase or lowercase, with or without enclosing quotes. The datepart can be specified as a literal or a host variable.
||A date/time expression from which the date part is to be returned. date-expression must contain a value of type datepart.
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
Note that DATENAME
is provided for Sybase and Microsoft SQL Server compatibility.
This function can also be invoked from Caché ObjectScript using the DATENAME()
argument 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 datepart
component names and abbreviations are not case-sensitive.
||Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
||0-999 (with precision of 3)
If you specify an invalid datepart
value as a literal, an SQLCODE -8 error code is issued. However, if you supply an invalid datepart
value as a host variable, no SQLCODE error is issued and the DATENAME
function returns a value of NULL.
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.
: Caché can be configured to determine the week of the year for a given date using either the Caché default algorithm or the ISO 8601 standard algorithm. For further details, refer to the WEEK
: The Caché default for weekday is to designate Sunday as first day of the week (weekday=1). However, you can configure the first day of the week to another value, or you can apply the ISO 8601 standard which designates Monday as first day of the week. For further details, refer to the DAYOFWEEK
: Caché returns three fractional digits of precision, with trailing zeroes removed. If the date-expression
has more than three fractional digits of precision, Caché truncates it to three digits.
can be specified as a quoted string or without quotes. These syntax variants perform slightly different operations:
A Caché %Date logical value (+$H)
A Caché %TimeStamp logical value (YYYY-MM-DD HH:MM:SS)
A Caché %String (or compatible) value
The Caché %String (or compatible) value can be in any of the following formats:
dd [yy]yy Mmm[mm]
yyyy Mmm[mm] dd
yyyy [dd] Mmm[mm]
is a slash (/), hyphen (-), or period (.).
Range validation is not performed for time components. Fractional seconds are truncated.
performs the following checks on input values. If a value fails a check, the null string is returned.
A valid date-expression
may consist of a date string (yyyy-mm-dd), a time string (hh:mm:ss), or a date and time string (yyyy-mm-dd hh:mm:ss). If both date and time are specified, both must be valid. For example, you can return a Year value if no time string is specified, but you cannot return a Year value if an invalid time string is specified.
A date string must be complete and properly formatted with the appropriate number of elements and digits for each element, and the appropriate separator character. For example, you cannot return a Year value if the Day value is omitted. Years must be specified as four digits.
A time string must be properly formatted with the appropriate separator character. Because a time value can be zero, you can omit one or more time elements (either retaining or omitting the separator characters) and these elements will be returned with a value of zero. Thus, 'hh:mm:ss', 'hh:mm:', 'hh:mm', 'hh::ss', 'hh::', 'hh', and ':::' are all valid. To omit the Hour element, date-expression
must not have a date portion of the string, and you must retain at least one separator character (:).
Date and time values must be within a valid range. Years: 1841 through 9999. Months: 1 through 12. Days: 1 through 31. Hours: 0 through 23. Minutes: 0 through 59. Seconds: 0 through 59.
The number of days in a month must match the month and year. For example, the date '0229' is only valid if the specified year is a leap year.
Most date and time values less than 10 may include or omit a leading zero. However, an Hour value of less than 10 must include the leading zero if it is part of a datetime string. Other non-canonical integer values are not permitted. Therefore, a Day value of '07' or '7' is valid, but '007', '7.0' or '7a' are not valid.
In the following example, each DATENAME
returns 'Wednesday' because that is the day of week ('dw') of the specified date:
SELECT DATENAME('dw','2004-02-25') AS DayName,
DATENAME(dw,'02/25/2004') AS DayName,
DATENAME('DW',59590) AS DayName
The following example returns 'December' because that is the month name ('mm') of the specified date:
SELECT DATENAME('mm','1999-12-20 12:00:00') AS MonthName
The following example returns '1999' (as a string) because that is the year ('yy') of the specified date:
SELECT DATENAME('yy','1999-12-20 12:00:00') AS Year
Note that the above examples use the abbreviations of the date parts. However, you can specify the full name, as in this example:
SELECT DATENAME('year','1999-12-20 12:00:00') AS Year
The following example returns the current quarter, week-of-year, and day-of-year. Each value is returned as a string:
SELECT DATENAME('Q',$HOROLOG) AS Q,
DATENAME('WK',$HOROLOG) AS WkCnt,
DATENAME('DY',$HOROLOG) AS DayCnt
&sql(SELECT DATENAME(:a,:b) INTO :c)
WRITE "this year is: ",c
The following example uses a subquery to returns records from Sample.Person whose day of birth was a Wednesday:
SELECT Name AS WednesdaysChild,DOB
FROM (SELECT Name,DOB,DATENAME('dw',DOB) AS Wkday FROM Sample.Person)
ORDER BY DOB