DATE_FORMAT option

Function

Sets the format used for dates retrieved from the database.

Allowed values

String

Scope

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

Default

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

Description

The format is a string using the following symbols:

Table 2-9: Symbols used in DATE_FORMAT string

Symbol

Description

yy

Two-digit year

yyyy

Four-digit year

mm

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

mmm

Three-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

Two-digit day of month

ddd

Three-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.

hh

Two-digit hours

nn

Two-digit minutes

ss[.s...s]

Seconds and parts of a second; up to six digits can follow the decimal point

aa

AM or PM (12 hour clock)

pp

PM if needed (12 hour clock)

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 multi-bye 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 (start_date, ‘yy’) + ‘?’
FROM employee;

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 upper case which will cause the substituted characters to also be in upper case. 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 symbols (MM, mm, DD, dd) all pad number with zeroes. 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 the following value:

1998/1/1

Examples

The following table illustrates DATE_FORMAT settings, together with the output from the following statement, executed on Thursday May 21, 1998:

     SELECT CURRENT DATE

DATE_FORMAT

SELECT CURRENT DATE

yyyy/mm/dd/ddd

1998/05/21/thu

jjj

141

mmm yyyy

may 1998

mm-yyyy

05-1998