Skip to main content

DAYNAME (SQL)

A date function that returns the name of the day of the week for a date expression.

Synopsis

{fn DAYNAME(date-expression)}

Description

DAYNAME returns the name of the day that corresponds to a specified date. The returned value is a character string with a maximum length of 15. The default day names returned are: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.

To change these default day name values, use the SET OPTION command with the WEEKDAY_NAME option.

The day name is calculated for an InterSystems IRIS date integer, a $HOROLOG or $ZTIMESTAMP value, an ODBC format date string, or a timestamp.

A date-expression timestamp can be either data type %Library.PosixTimeOpens in a new tab (an encoded 64-bit signed integer), or data type %Library.TimeStampOpens in a new tab (yyyy-mm-dd hh:mm:ss.fff).

The time portion of the timestamp is not evaluated and can be omitted.

DAYNAME checks that the date supplied is a valid date. The year must be between 0001 and 9999 (inclusive), the month 01 through 12, and the day appropriate for that month (for example, 02/29 is only valid on leap years). If the date is not valid, DAYNAME issues an SQLCODE -400 error (Fatal error occurred).

The same day of week information can be returned by using the DATENAME function. You can use TO_DATE to retrieve a day name or day name abbreviation with other date elements. To return an integer corresponding to the day of the week, use DAYOFWEEK DATEPART or TO_DATE.

This function can also be invoked from ObjectScript using the DAYNAME()Opens in a new tab method call:

$SYSTEM.SQL.Functions.DAYNAME(date-expression)

Arguments

date-expression

An expression that evaluates to either an InterSystems IRIS date integer, an ODBC date, or a timestamp. This expression can be the name of a column, the result of another scalar function, or a date or timestamp literal.

Examples

The following examples both return the character string Wednesday because the day of the date (February 21, 2018) is a Wednesday. The first example takes a timestamp string:

SELECT {fn DAYNAME('2018-02-21 12:35:46')} AS Weekday

The second example takes an InterSystems IRIS date integer:

SELECT {fn DAYNAME(64700)} AS Weekday

The following examples all return the name of the current day of the week:

SELECT {fn DAYNAME({fn NOW()})} AS Wd_Now,
       {fn DAYNAME(CURRENT_DATE)} AS Wd_CurrDate,
       {fn DAYNAME(CURRENT_TIMESTAMP)} AS Wd_CurrTstamp,
       {fn DAYNAME($ZTIMESTAMP)} AS Wd_ZTstamp,
       {fn DAYNAME($HOROLOG)} AS Wd_Horolog

Note that $ZTIMESTAMP returns Coordinated Universal Time (UTC). The other time-expression values return the local time. This may affect the DAYNAME value.

The following example shows how DAYNAME responds to an invalid date (the year 2017 was not a leap year):

SELECT {fn DAYNAME("2017-02-29")}

See Also

FeedbackOpens in a new tab