Sets the format used for dates retrieved from the database.
String
'YYYY-MM-DD'. This corresponds to ISO date format specifications.
Can be set for an individual connection or the PUBLIC group. Takes effect immediately.
The format is a string using the following symbols:
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. |
hh |
2-digit hours |
nn |
2-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 |
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 ('1998/01/01' as date ), 'yyyy/Mm/Dd' )
returns the following value:
1998/1/1
Table 2-10 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 |