Sub topic 2
Reading and Writing Oracle Format Models
Prerequisites and the basic idea
This chapter assumes that you can write basic SELECT, WHERE, and ORDER BY statements. You should also know the NUMBER, VARCHAR2, and DATE data types and understand that DUAL is Oracle's one-row helper table.
A format model is a pattern that tells Oracle how to read or display a value. It is a character string made from format elements, such as YYYY for a four-digit year or 999 for number positions.
The same model can be used in two directions:
TO_CHARconverts a date or number into text.TO_DATEorTO_NUMBERreads text and converts it into a database value.
The model must describe the value accurately. It is not a general-purpose display mask that Oracle can interpret any way you want.
flowchart TD
A[Database value] -->|TO_CHAR with format model| B[Character string]
B -->|TO_DATE or TO_NUMBER with matching model| A
C[Format elements] --> A
C --> BDate format elements
Date format elements identify parts of a DATE value. Common elements include:
| Element | Meaning | Example |
|---|---|---|
| `YYYY` | Four-digit year | `2026` |
| `MM` | Two-digit month | `11` |
| `DD` | Day of month | `28` |
| `HH24` | Hour from 00 through 23 | `14` |
| `MI` | Minute | `30` |
| `SS` | Second | `15` |
| `MON` | Abbreviated month name | `NOV` |
| `MONTH` | Full month name | `NOVEMBER` |
| `DY` | Abbreviated day name | `SAT` |
| `DAY` | Full day name | `SATURDAY` |
Punctuation in the model usually appears unchanged in the result. This model produces a predictable, sortable text value:
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS now_text
FROM dual;The letters in HH24 matter. HH uses a 12-hour clock, while HH24 uses a 24-hour clock. If you use HH with 14, the model does not describe the value correctly. For 12-hour output, pair HH with AM or PM.
Literal words can be enclosed in double quotation marks:
SELECT TO_CHAR(DATE '2026-11-28',
'FMDay, DD "of" Month YYYY') AS description
FROM dual;FM is a modifier that removes padding from text elements such as Day and Month. Without it, Oracle may add trailing blanks to make names fit their maximum lengths.
Number format elements
Number models describe digits, grouping, decimals, signs, and currency. Common elements include:
| Element | Meaning |
|---|---|
| `9` | A digit position; unnecessary leading positions become blanks |
| `0` | A digit position that displays zero when needed |
| `G` | Group separator, such as a comma |
| `D` | Decimal separator |
| `L` | Local currency symbol |
| `S` | Leading or trailing sign |
This query formats a number with grouping and two decimal places:
SELECT TO_CHAR(1234567.891, 'FM9G999G999D00') AS amount
FROM dual;The G and D elements are preferable to hard-coded commas and periods when the format should follow Oracle's numeric settings. The 00 requires two digits after the decimal point, so Oracle rounds the value to two places.
Use 0 when a leading zero is required:
SELECT TO_CHAR(0.5, 'FM990D00') AS amount
FROM dual;This returns 0.50. The FM modifier removes the blank that number models can otherwise reserve for a positive or negative sign.
Matching models when parsing
When converting text into a date or number, build the model element by element so it matches the input:
SELECT TO_DATE('28-11-2026 14:30',
'DD-MM-YYYY HH24:MI') AS parsed_date,
TO_NUMBER('1,234.56',
'9G999D99',
'NLS_NUMERIC_CHARACTERS='',.''') AS parsed_number
FROM dual;The separators, order, and number of elements should agree with the input. A mismatch can cause errors such as ORA-01861, which means a literal does not match the format string.
For strict parsing, the FX modifier requests an exact match:
SELECT TO_DATE('2026-11-28', 'FXYYYY-MM-DD') AS parsed_date
FROM dual;Practice
Exercise
Which date format model parses the text 2026/11/28 14:30: DD-MM-YYYY HH24:MI or YYYY/MM/DD HH24:MI?
Show answer
YYYY/MM/DD HH24:MI, because the order and separators match the input exactly.