ORACLE - Date Format
Default Date Format
The default date format is specified either explicitly with the initialization parameter NLS_DATE_FORMAT or implicitly with the initialization parameter NLS_TERRITORY.
You can change the default date format for your session with the ALTER SESSION command.
Maximum Length
The total length of a date format model cannot exceed 22 characters.
Date Format Elements
A date format model is composed of one or more date format elements . 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. Only some of the date format elements can be used in the TO_DATE function
Table 3-15 Date Format Elements
Element | Specify in TO_DATE? | Meaning |
- / , . ; : 'text' | Yes | Punctuation and quoted text is reproduced in the result. |
AD A.D. | Yes | AD indicator with or without periods. |
AM A.M. | Yes | Meridian indicator with or without periods. |
BC B.C. | Yes | BC indicator with or without periods. |
CC SCC | No | One greater than the first two digits of a four-digit year; "S" prefixes BC dates with "-". For example, '20' from '1900'. |
D | Yes | Day of week (1-7). |
DAY | Yes | Name of day, padded with blanks to length of 9 characters. |
DD | Yes | Day of month (1-31). |
DDD | Yes | Day of year (1-366). |
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). |
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. |
IYY IY I | 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; JAN = 01) |
MON | Yes | Abbreviated name of month. |
MONTH | Yes | Name of month, padded with blanks to length of 9 characters. |
PM P.M. | No | Meridian indicator with or without periods. |
Q | No | Quarter of year (1, 2, 3, 4; JAN-MAR = 1) |
RM | Yes | Roman numeral month (I-XII; JAN = I). |
RR | Yes | Given a year with 2 digits, returns a year in the next century if the year is <50 and the last 2 digits of the current year are >=50; returns a year in the preceding century if the year is >=50 and the last 2 digits of the current year are <50. |
RRRR | Yes | Round year. Accepts either 4-digit or 2-digit input. If 2-digit, provides the same return as RR. If you don't want this functionality, simply enter the 4-digit year. |
SS | Yes | Second (0-59). |
SSSSS | Yes | Seconds past midnight (0-86399). |
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. |
Y,YYY | Yes | Year with comma in this position. |
YEAR SYEAR | No | Year, spelled out; "S" prefixes BC dates with "-". |
YYYY SYYYY | Yes | 4-digit year; "S" prefixes BC dates with "-". |
YYY YY Y | Yes | Last 3, 2, or 1 digit(s) of year. |
Table 3-16 The RR Date Element Format
If the specified two-digit year is | |||
| 0 - 49 | 50 - 99 | |
If the last two digits of the current year are: | 0-49 | The return date is in the current century. | The return date is in the preceding century. |
50-99 | The return date is in the next century. | The return date is in the current century. |
The following example demonstrates the behavior of the RR date format element.
Example 1
Assume these queries are issued between 1950 and 1999:
SELECT TO_CHAR(TO_DATE('27-OCT-95', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
Year
----
1995
SELECT TO_CHAR(TO_DATE('27-OCT-17', 'DD-MON-RR') ,'YYYY') "Year";
FROM DUAL;
Year
----
2017
Example 2
Assume these queries are issued between 2000 and 2049:
SELECT TO_CHAR(TO_DATE('27-OCT-95', 'DD-MON-RR') ,'YYYY') "Year";
FROM DUAL;
Year
----
1995
SELECT TO_CHAR(TO_DATE('27-OCT-17', 'DD-MON-RR') ,'YYYY') "Year";
FROM DUAL;
Year
----
2017
Note that the queries return the same values regardless of whether they are issued before or after the year 2000. The RR date format element allows you to write SQL statements that will return the same values after the turn of the century.
Table 3-17 Date Format Element Suffixes
Suffix | Meaning | Example Element | Example Value |
TH | Ordinal Number | DDTH | 4TH |
SP | Spelled Number | DDSP | FOUR |
SPTH or THSP | Spelled, ordinal number | DDSPTH | FOURTH |
Format Model Modifiers
You can use the FM and FX modifiers in format models for the TO_CHAR function to control blank padding and exact format checking.
FM
"Fill mode". This modifier suppresses blank padding in the return value of the TO_CHAR function:
· In a date format element of a TO_CHAR function, this modifier suppresses blanks in subsequent character elements (such as MONTH) and suppresses leading and trailing zeroes for subsequent number elements (such as MI) in a date format model. Without FM, the result of a character element is always right padded with blanks to a fixed length, and leading zeroes are always returned for a number element. With FM, because there is no blank padding, the length of the return value may vary
· In a number format element of a TO_CHAR function, this modifier suppresses blanks added to the left of the number, so that the result is left-justified in the output buffer. Without FM, the result is always right-justified in the buffer, resulting in blank-padding to the left of the number.
Example 1
The following statement uses a date format model to return a character expression:
SELECT TO_CHAR(SYSDATE, 'fmDDTH')' of 'TO_CHAR
(SYSDATE, 'Month')', 'TO_CHAR(SYSDATE, 'YYYY') "Ides"
FROM DUAL;
Ides
------------------
3RD of April, 1995
Note that the statement above also uses the FM modifier. If FM is omitted, the month is blank-padded to nine characters:
SELECT TO_CHAR(SYSDATE, 'DDTH')' of '
TO_CHAR(Month, YYYY') "Ides"
FROM DUAL;
Ides
-----------------------
03RD of April , 1995
No hay comentarios:
Publicar un comentario