DATE_FORMAT Option

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

Option can be set at the database (PUBLIC) or user level. When set at the database level, the value becomes the default for any new user, but has no impact on existing users. When set at the user level, overrides the PUBLIC value for that user only. No system privilege is required to set option for self. System privilege is required to set at database level or at user level for any user other than self.

Requires the SET ANY PUBLIC OPTION system privilege to set this option. Can be set temporary for an individual connection or for the PUBLIC role. Takes effect immediately.

Description

The format is a string using these symbols:

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

Note: Multibyte 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 ('2011/01/01' as date ), 'yyyy/Mm/Dd' )

returns this value:

2011/1/1

Examples

This table illustrates DATE_FORMAT settings, together with the output from this statement, executed on Saturday May 21, 2011:

SELECT CURRENT DATE
DATE_FORMAT Settings

DATE_FORMAT

SELECT CURRENT DATE

yyyy/mm/dd/ddd

2011/05/21/sat

jjj

141

mmm yyyy

may 2011

mm-yyyy

05-2011

Related reference
RETURN_DATE_TIME_AS_STRING Option
TIME_FORMAT Option