DATE_FORMAT option

Function

Sets the format used for dates retrieved from the database.

Allowed values

String

Default

'YYYY-MM-DD'. This corresponds to ISO date format specifications.

Scope

Can be set for an individual connection or the PUBLIC group. Takes effect immediately.

Description

The format is a string using these symbols:

Table 2-9: Symbols used in DATE_FORMAT string

Symbol

Description

yy

2-digit year

yyyy

4-digit year

mm

2-digit month, or 2-digit minutes if following a colon (as in 'hh:mm')

mmm

3-character name of month

mmmm[m...]

Character long form for months—as many characters as there are m's, until the number of m’s specified exceeds the number of characters in the month’s name.

d

Single-digit day of week, (0 = Sunday, 6 = Saturday)

dd

2-digit day of month

ddd

3-character name of the day of week.

dddd[d...]

Character long form for day of the week—as many characters as there are d's, until the number of d’s specified exceeds the number of characters in the day’s name.

jjj

Day of the year, from 1 to 366

NoteMultibyte characters are not supported in date format strings. Only single-byte characters are allowed, even when the collation order of the database is a multibyte collation order like 932JPN. Use the concatenation operator to include multibyte characters in date format strings. For example, if '?' represents a multibyte character, use the concatenation operator to move the multibyte character outside of the date format string:

SELECT DATEFORMAT (StartDate, 'yy') + '?'
FROM Employees;

Each symbol is substituted with the appropriate data for the date being formatted. Any format symbol that represents character rather than digit output can be put in uppercase which causes the substituted characters to also be in uppercase. For numbers, using mixed case in the format string suppresses leading zeros.

You can control the padding of numbers by changing the case of the symbols. Same-case values (MM, mm, DD, or dd) all pad number with zeros. Mixed-case (Mm, mM, Dd, or dD) cause the number to not be zero-padded; the value takes as much room as required. For example:

SELECT dateformat ( cast ('1998/01/01' as date ), 'yyyy/Mm/Dd' )

returns this value:

1998/1/1

Examples

Table 2-10 illustrates DATE_FORMAT settings, together with the output from this statement, executed on Thursday May 21, 1998:

     SELECT CURRENT DATE
Table 2-10: DATE_FORMAT settings

DATE_FORMAT

SELECT CURRENT DATE

yyyy/mm/dd/ddd

1998/05/21/thu

jjj

141

mmm yyyy

may 1998

mm-yyyy

05-1998

See also

“Setting options”

“RETURN_DATE_TIME_AS_STRING option”

“TIME_FORMAT option”