Sets the format used for dates retrieved from the database.
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.
The format is a string using these 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 |
jjj | Day of the year, from 1 to 366 |
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