Oracle PL/SQL Programming (2014)
Appendix C. Date Format Models
Table C-1 lists the date format model elements that you can use with the conversion functions TO_CHAR, TO_DATE, TO_TIMESTAMP, and TO_TIMESTAMP_TZ. Some of the model elements in Table C-1 are also used with ROUND and TRUNC.
You have the option of specifying default date and timestamp formats at the session level, a capability that can come in handy if your particular needs differ from those of the majority of database users. Use the ALTER SESSION command to specify session-level default date and timestamp formats. The following example works in Oracle8i Database or higher, and sets the default date format to MM/DD/YYYY:
BEGIN
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_DATE_FORMAT=''MM/DD/YYYY''';
END;
To check the default date format in effect for your session at any given time, issue the following query against the NLS_SESSION_PARAMETERS data dictionary view:
SELECT value
FROM nls_session_parameters
WHERE parameter='NLS_DATE_FORMAT';
To set or check default timestamp formats, use NLS_TIMESTAMP_FORMAT and NLS_TIMESTAMP_TZ_FORMAT.
Some elements in Table C-1 apply only when translating datetime values from Oracle’s internal format into character strings, and not vice versa. Such elements can’t be used in a default date model (e.g., with NLS_DATE_FORMAT) because the default date model applies to conversions in both directions. These elements are noted as “Output only” in the table.
Table C-1. Date format model elements
Element |
Description |
Other text |
Any punctuation, such as a comma (,), slash (/), or hyphen (-), will be reproduced in the formatted output of the conversion. You can also include text within double quotes (“ ”) and the text will be represented as entered in the converted value. |
A.M. or P.M. |
The meridian indicator (morning or evening) with periods. |
AM or PM |
The meridian indicator (morning or evening) without periods. |
B.C. or A.D. |
The B.C. or A.D. indicator, with periods. |
BC or AD |
The B.C. or A.D. indicator, without periods. |
CC and SCC |
The century. If the SCC format is used, any B.C. dates are prefaced with a minus sign (–). Output only. |
D |
The day of the week, from 1 through 7. The day of the week that is decreed the first day is specified implicitly by the NLS_TERRITORY initialization parameter for the database instance. |
DAY, Day, or day |
The name of the day in uppercase, mixed case, or lowercase format. |
DD |
The day of the month, from 1 through 31. |
DDD |
The day of the year, from 1 through 366. |
DL |
Long date format. Depends on the current values of NLS_TERRITORY and NLS_LANGUAGE. May be used alone or with TS, but not with any other elements. |
DS |
Short date format. Depends on the current values of NLS_TERRITORY and NLS_LANGUAGE. May be used alone or with TS, but not with any other elements. |
DY, Dy, or dy |
The abbreviated name of the day, as in TUE for Tuesday. |
E |
The abbreviated era name. Valid only for the following calendars: Japanese Imperial, ROC Official, and Thai Buddha. |
EE |
The full era name. |
FF |
The fractional seconds. Only valid when used with TIMESTAMP values. The number of digits returned will correspond to the precision of the datetime being converted. Always use FF (two Fs) regardless of the number of decimal digits you wish to see or use. Any other number of Fs is invalid. |
FF1..FF9 |
Same as FF, but the digit (1..9) controls the number of decimal digits used for fractional seconds. Use FF1 to see one digit past the decimal point, FF2 to see two digits past, and so forth. |
FM |
Element that toggles suppression of blanks in output from conversion. (FM stands for Fill Mode.) |
FX |
Element that requires exact pattern matching between data and format model. (FX stands for Format eXact.) |
HH or HH12 |
The hour of the day, from 1 through 12. Output only. |
HH24 |
The hour of the day, from 0 through 23. |
IW |
The week of the year, from 1 through 52 or 1 through 53, based on the ISO standard. Output only. |
IYY or IY or I |
The last three, two, or one digits of the ISO standard year. Output only. |
IYYY |
The four-digit ISO standard year. Output only. |
J |
The Julian day format of the date (counted as the number of days since January 1, 4712 B.C., the earliest date supported by the Oracle database). |
MI |
The minutes component of the datetime value, from 0 through 59. |
MM |
The number of the month in the year, from 01 through 12. January is month number 01, September is 09, etc. |
MON, Mon, or mon |
The abbreviated name of the month, as in JAN for January. This also may be in upper-, mixed-, or lowercase format. |
MONTH, Month, or month |
The name of the month, in upper-, mixed-, or lowercase format. |
Q |
The quarter of the year, from 1 through 4. January through March are in the first quarter, April through June in the second quarter, and so on. Output only. |
RM |
The Roman numeral representation of the month number, from I through XII. January is I, September is IX, and so on. Output only. |
RR |
The last two digits of the year. This format displays years in centuries other than our own. |
RRRR |
Same as RR when used for output; accepts four-digit years when used for input. |
SCC or CC |
The century. If the SCC format is used, any B.C. dates are prefaced with a minus sign (–). Output only. |
SP |
Suffix that converts a number to its spelled format. This element can appear at the end of any element that results in a number. For example, a model such as “DDth-Mon-Yyyysp” results in output such as “15th-Nov-One Thousand Nine Hundred Sixty-One”. The return value is always in English, regardless of the date language. (Note that Yyyy resulted in mixed-case words.) |
SPTH or THSP |
Suffix that converts a number to its spelled and ordinal format; for example, 4 becomes FOURTH and 1 becomes FIRST. This element can appear at the end of any element that results in a number. For example, a model such as “Ddspth Mon, Yyyysp” results in output such as “Fifteenth Nov, One Thousand Nine Hundred Sixty-One”. The return value is always in English, regardless of the date language. |
SS |
The seconds component of the datetime value, from 0 through 59. |
SSSSS |
The number of seconds since midnight of the time component. Values range from 0 through 86399, with each hour comprising 3,600 seconds. |
SYEAR, YEAR, SYear, Year, syear, or year |
The year spelled out in words (e.g., “two thousand two”). The S prefix places a negative sign in front of B.C. dates. The format may be uppercase, mixed-case, or lowercase. Output only. |
SYYYY or YYYY |
The four-digit year. If the SYYYY format is used, any B.C. dates are prefaced with a minus sign (–). |
TH |
Suffix that converts a number to its ordinal format; for example, 4 becomes 4th and 1 becomes 1st. This element can appear at the end of any element that results in a number. For example, “DDth-Mon-YYYY’ results in output such as “15th-Nov-1961”. The return value is always in English, regardless of the date language. |
TS |
Short time format. Depends on the current values of NLS_TERRITORY and NLS_LANGUAGE. May be used alone or with either DL or DS, but not with any other elements. |
TZD |
The abbreviated time zone name—for example, EST, PST. This is an input-only format, which may seem odd at first. |
TZH |
The time zone hour displacement. For example, −5 indicates a time zone five hours earlier than UTC. |
TZM |
The time zone minute displacement. For example, −5:30 indicates a time zone that is five hours, thirty minutes earlier than UTC. A few such time zones do exist. |
TZR |
The time zone region. For example, “US/Eastern” is the region in which EST (Eastern Standard Time) and EDT (Eastern Daylight Time) are valid. |
W |
The week of the month, from 1 through 5. Week 1 starts on the first day of the month and ends on the seventh. Output only. |
WW |
The week of the year, from 1 through 53. Output only. |
X |
The local radix character. In American English, this is a period (.). This element can be placed in front of FF so that fractional seconds are properly interpreted and represented. |
Y,YYY |
The four-digit year with a comma. |
YYY or YY or Y |
The last three, two, or one digits of the year. The current century is the default when you’re using these elements to convert a character string value into a date. |
Whenever a date format returns a spelled value—words rather than numbers, as with MONTH, MON, DAY, DY, AM, and PM—the language used to spell these words is determined by the Globalization Support (formerly National Language Support) parameters NLS_DATE_LANGUAGE and NLS_LANGUAGE, or by the optional date language argument you can pass to both TO_ CHAR and TO_DATE.
ISO DATES
The IYY and IW elements represent the ISO (International Standards Organization) year and week. The ISO calendar is a good example of “design by committee.” The first day of the ISO year is always a Monday and is determined by the following rules:
§ When January 1 falls on a Monday, the ISO year begins on the same day.
§ When January 1 falls on a Tuesday through Thursday, the ISO year begins on the preceding Monday.
§ When January 1 falls on a Friday through Sunday, the ISO year begins on the following Monday.
These rules lead to some strange situations. For example, 31-Dec-2008 is considered to be the first day of ISO year 2009, and if you display that date using the IYYY format, 31-Dec-2009 is exactly what you’ll get.
ISO weeks always begin on Mondays and are numbered from the first Monday of the ISO year.
Here are some examples of date format models composed of the preceding format elements:
'Month DD, YYYY'
'MM/DD/YY Day A.M.'
'Year Month Day HH24:MI:SS'
'J'
'SSSSS-YYYY-MM-DD'
'"A beautiful summer morning on the" DDth" day of "Month'
You can use the format elements in any combination, in any order. Older releases of Oracle allowed you to specify the same date element twice. For example, the model “Mon (MM) DD, YYYY” specifies the month twice. However, you can specify an element only once in a format model. For example, you can specify only one of MONTH, MON, and MM because all three refer to the month.
See the description of the TO_CHAR and TO_DATE functions in Chapter 10 for more examples of the use and resulting values of date format models.