You can use datetime format models in the following functions:
-
In the
TO_*
datetime functions to translate a character value that is in a format other than the default format into a datetime value. (TheTO_
* datetime functions areTO_CHAR
,TO_DATE
,TO_TIMESTAMP
,TO_TIMESTAMP_TZ
,TO_YMINTERVAL
, andTO_DSINTERVAL
.) -
In the
TO_CHAR
function to translate a datetime value that is in a format other than the default format into a string (for example, to print the date from an application)
The total length of a datetime format model cannot exceed 22 characters.
The default datetime formats are specified either explicitly with the initialization parameter NLS_DATE_FORMAT
or implicitly with the initialization parameterNLS_TERRITORY
. You can change the default datetime formats for your session with the ALTER
SESSION
statement.
See Also:
ALTER SESSION and Oracle Database Globalization Support Guide for information on the NLS parametersDatetime Format Elements
A datetime format model is composed of one or more datetime format elements as listed in Table 2-19, "Attributes of the XMLFormat Object".
-
For input format models, format items cannot appear twice, and format items that represent similar information cannot be combined. For example, you cannot use 'SYYYY' and 'BC' in the same format string.
-
Some of the datetime format elements cannot be used in the
TO_*
datetime functions, as noted in Table. -
The following datetime format elements can be used in timestamp and interval format models, but not in the original
DATE
format model:FF
,TZD
,TZH,
TZM
, andTZR
. -
Many datetime format elements are blank padded to a specific length. Please refer to the format model modifier FM for more information.
Uppercase Letters in Date Format Elements
Capitalization in a spelled-out word, abbreviation, or Roman numeral follows capitalization in the corresponding format element. For example, the date format model 'DAY' produces capitalized words like 'MONDAY'; 'Day' produces 'Monday'; and 'day' produces 'monday'.
Punctuation and Character Literals in Datetime Format Models
You can include these characters in a date format model:
-
Punctuation such as hyphens, slashes, commas, periods, and colons
-
Character literals, enclosed in double quotation marks
These characters appear in the return value in the same location as they appear in the format model.
Datetime Format Elements
Element | Specify in TO_* datetime functions? | Description |
---|---|---|
-/,.;:"text" | Yes | Punctuation and quoted text is reproduced in the result. |
ADA.D. | Yes | AD indicator with or without periods. |
AMA.M. | Yes | Meridian indicator with or without periods. |
BCB.C. | Yes | BC indicator with or without periods. |
CCSCC | No | Century.
For example, 2002 returns 21; 2000 returns 20. |
D | Yes | Day of week (1-7). |
DAY | Yes | Name of day, padded with blanks to display width of the widest name of day in the date language used for this element. |
DD | Yes | Day of month (1-31). |
DDD | Yes | Day of year (1-366). |
DL | Yes | Returns a value in the long date format, which is an extension of Oracle Database's Restriction: You can specify this format only with the |
DS | Yes | Returns a value in the short date format. Makes the appearance of the date components (day name, month number, and so forth) depend on the Restriction: You can specify this format only with the |
DY | Yes | Abbreviated name of day. |
E | No | Abbreviated era name (Japanese Imperial, ROC Official, and Thai Buddha calendars). |
EE | No | Full era name (Japanese Imperial, ROC Official, and Thai Buddha calendars). |
FF [1..9] | Yes | Fractional seconds; no radix character is printed (use the X format element to add the radix character). Use the numbers 1 to 9 after FF to specify the number of digits in the fractional second portion of the datetime value returned. If you do not specify a digit, then Oracle Database uses the precision specified for the datetime datatype or the datatype's default precision. Examples:
|
FM | Yes | Returns a value with no leading or trailing blanks. See Also: Additional discussion on this format model modifier in the Oracle Database SQL Reference |
FX | Yes | Requires exact matching between the character data and the format model. See Also: Additional discussion on this format model modifier in the Oracle Database SQL Reference |
HH | Yes | Hour of day (1-12). |
HH12 | No | Hour of day (1-12). |
HH24 | Yes | Hour of day (0-23). |
IW | No | Week of year (1-52 or 1-53) based on the ISO standard. |
IYYIYI | No | Last 3, 2, or 1 digit(s) of ISO year. |
IYYY | No | 4-digit year based on the ISO standard. |
J | Yes | Julian day; the number of days since January 1, 4712 BC. Number specified with J must be integers. |
MI | Yes | Minute (0-59). |
MM | Yes | Month (01-12; January = 01). |
MON | Yes | Abbreviated name of month. |
MONTH | Yes | Name of month, padded with blanks to display width of the widest name of month in the date language used for this element. |
PMP.M. | No | Meridian indicator with or without periods. |
Q | No | Quarter of year (1, 2, 3, 4; January - March = 1). |
RM | Yes | Roman numeral month (I-XII; January = I). |
RR | Yes | Lets you store 20th century dates in the 21st century using only two digits. See Also: Additional discussion on |
RRRR | Yes | Round year. Accepts either 4-digit or 2-digit input. If 2-digit, provides the same return as RR. If you do not want this functionality, then enter the 4-digit year. |
SS | Yes | Second (0-59). |
SSSSS | Yes | Seconds past midnight (0-86399). |
TS |
| Returns a value in the short time format. Makes the appearance of the time components (hour, minutes, and so forth) depend on the Restriction: You can specify this format only with the |
TZD | Yes | Daylight savings information. The TZD value is an abbreviated time zone string with daylight savings information. It must correspond with the region specified in TZR. Example: |
TZH | Yes | Time zone hour. (See Example: |
TZM | Yes | Time zone minute. (See Example: |
TZR | Yes | Time zone region information. The value must be one of the time zone regions supported in the database. Example: US/Pacific |
WW | No | Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year. |
W | No | Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh. |
X | Yes | Local radix character. Example: |
Y,YYY | Yes | Year with comma in this position. |
YEARSYEAR | No | Year, spelled out; |
YYYYSYYYY | Yes | 4-digit year; |
YYYYYY | Yes | Last 3, 2, or 1 digit(s) of year. |
Oracle returns an error if an alphanumeric character is found in the date string where a punctuation character is found in the format string. For example, the following format string returns an error:
TO_CHAR (TO_DATE('0297','MM/YY'), 'MM/YY')
Datetime Format Elements and Globalization Support
The functionality of some datetime format elements depends on the country and language in which you are using Oracle Database. For example, these datetime format elements return spelled values:
-
MONTH
-
MON
-
DAY
-
DY
-
BC or AD or B.C. or A.D.
-
AM or PM or A.M or P.M.
The language in which these values are returned is specified either explicitly with the initialization parameter NLS_DATE_LANGUAGE
or implicitly with the initialization parameter NLS_LANGUAGE
. The values returned by the YEAR
and SYEAR
datetime format elements are always in English.
The datetime format element D
returns the number of the day of the week (1-7). The day of the week that is numbered 1 is specified implicitly by the initialization parameter NLS_TERRITORY
.