Sub topic 2
Reading and writing format models
This chapter assumes you can write basic SELECT queries, know the NUMBER, VARCHAR2, and DATE types, and can run SQL in SQL*Plus or SQL Developer. The examples use DUAL and assume an English-language session unless stated.
What a format model is
A format model is a string of elements that describes the layout of a date, timestamp, or number written as text. It is the second argument of TO_CHAR, TO_DATE, and TO_NUMBER.
The same model works in both directions:
- With
TO_CHAR, the model is a template. Oracle fills it in from a value and returns text. - With
TO_DATEandTO_NUMBER, the model is a pattern. Oracle matches the input text against it and returns a value.
If you omit the model, Oracle falls back to a session setting such as NLS_DATE_FORMAT. Two sessions can then give different results for the same query. Passing an explicit model removes that dependency.
Anatomy of a date model
A date model is a sequence of elements and separators.
- An element is a code such as
YYYYorMIthat stands for one part of the date. - Punctuation (
- / , . ; :and space) is copied to the output unchanged. - Literal text goes inside double quotes, for example
"of". Without the quotes, Oracle tries to read letters as elements and raises an error.
| Element | Meaning | Example for 2026-11-28 14:05:09 |
|---|---|---|
| `YYYY` | 4-digit year | 2026 |
| `YY` | 2-digit year | 26 |
| `MM` | Month number | 11 |
| `MON` | Abbreviated month name | NOV |
| `MONTH` | Full month name, padded to 9 characters | NOVEMBER |
| `DD` | Day of month | 28 |
| `DDD` | Day of year | 332 |
| `DY` | Abbreviated day name | SAT |
| `DAY` | Full day name, padded | SATURDAY |
| `Q` | Quarter | 4 |
| `HH24` | Hour, 00 to 23 | 14 |
| `HH` or `HH12` | Hour, 01 to 12 | 02 |
| `MI` | Minutes | 05 |
| `SS` | Seconds | 09 |
| `AM` or `PM` | Meridian indicator | PM |
WITH t AS (SELECT DATE '2026-11-28' AS d FROM dual)
SELECT TO_CHAR(d, 'DD/MM/YYYY') AS numeric_style, -- 28/11/2026
TO_CHAR(d, 'Dy DD Mon YY') AS short_text, -- Sat 28 Nov 26
TO_CHAR(d, '"Quarter" Q "of" YYYY') AS with_literals, -- Quarter 4 of 2026
TO_CHAR(d, 'DDD') AS day_of_year -- 332
FROM t;Capitalization controls output case
The case you use for a name element decides the case of the output.
| Model | Output |
|---|---|
| `MONTH` | NOVEMBER |
| `Month` | November |
| `month` | november |
The same rule applies to DAY, DY, and MON. The FM prefix in the next example removes the blank padding. Full coverage of FM comes in a later chapter.
SELECT TO_CHAR(DATE '2026-11-28', 'FMDay, DD Month YYYY') AS long_date
FROM dual;
-- Saturday, 28 November 2026MM is the month, MI is the minute
MM always means month and MI always means minutes. A model such as HH24:MM is valid and does not raise an error. It prints the month number where the minutes should be.
SELECT TO_CHAR(TO_DATE('2026-11-28 14:05:09', 'YYYY-MM-DD HH24:MI:SS'),
'HH12:MI:SS AM') AS clock_time -- 02:05:09 PM
FROM dual;A DATE always carries a time of day. If the input has none, TO_DATE sets it to midnight (00:00:00), and TO_CHAR with a time element will show that midnight.
Anatomy of a number model
A number model describes digit positions, not digits themselves.
| Element | Meaning |
|---|---|
| `9` | One digit position. Leading zeros are shown as blanks. |
| `0` | One digit position. Leading zeros are shown as `0`. |
| `,` | Group separator at that fixed position |
| `.` | Decimal point at that fixed position |
| `G` | Group separator from `NLS_NUMERIC_CHARACTERS` |
| `D` | Decimal separator from `NLS_NUMERIC_CHARACTERS` |
| `$` | Dollar sign |
| `L` | Local currency symbol from `NLS_CURRENCY` |
| `MI` | Trailing minus sign for negative values |
| `PR` | Negative values in angle brackets, such as `<5>` |
| `S` | Leading sign, `+` or `-` |
, and . are literal characters at fixed positions. G and D take their characters from the session or from the nlsparam argument, which makes a model portable across locales. The next chapters return to this.
SELECT TO_CHAR(1234567.891, 'FM9,999,999.00') AS grouped, -- 1,234,567.89
TO_CHAR(42, 'FM0000') AS zero_pad, -- 0042
TO_CHAR(0.5, 'FM990.00') AS lead_zero, -- 0.50
TO_CHAR(-1234.5, 'FM9,999.00MI') AS trailing, -- 1,234.50-
TO_CHAR(-5, 'FM999PR') AS brackets -- <5>
FROM dual;Rounding and overflow
TO_CHAR rounds to the number of decimal positions in the model. It does not truncate.
SELECT TO_CHAR(1234.567, 'FM9999.99') AS rounded -- 1234.57
FROM dual;If the model has too few integer positions for the value, Oracle does not raise an error. It returns a string of # characters.
SELECT TO_CHAR(12345, '9999') AS too_small -- #####
FROM dual;Without FM, a number result also reserves one leading position for the sign. That is why TO_CHAR(42, '9999') returns five characters, not four.
Reading: how Oracle matches input to a model
TO_DATE and TO_NUMBER walk through the model and the input text together, element by element. The parsing rules are more lenient than most people expect.
Default (loose) matching
In the default mode:
- Any punctuation in the input matches any punctuation in the model.
- Leading zeros can be omitted.
- Name elements are case-insensitive.
SELECT TO_CHAR(TO_DATE('2026/11/28', 'YYYY-MM-DD'), 'YYYY-MM-DD') AS other_separator, -- 2026-11-28
TO_CHAR(TO_DATE('2026-1-5', 'YYYY-MM-DD'), 'YYYY-MM-DD') AS short_parts, -- 2026-01-05
TO_CHAR(TO_DATE('28 november 2026', 'DD MONTH YYYY',
'NLS_DATE_LANGUAGE=ENGLISH'), 'YYYY-MM-DD') AS lower_case -- 2026-11-28
FROM dual;Loose matching can hide bad data. A feed that mixes 2026/11/28 and 2026-11-28 parses cleanly with one model, even if you wanted to reject one of them.
Exact matching with FX
Put FX at the start of the model to require an exact match. Punctuation must be identical, and every numeric element must have its full number of digits.
SELECT TO_DATE('2026/11/28', 'FXYYYY-MM-DD') FROM dual;
-- ORA-01861: literal does not match format string
SELECT TO_DATE('2026-1-5', 'FXYYYY-MM-DD') FROM dual;
-- ORA-01861: literal does not match format stringUse FX when you want a strict check of incoming text, such as file imports. Leave it off when you accept human-typed input.
Rules that raise errors
Some mistakes are caught at parse time, even in loose mode.
| Mistake | Typical error |
|---|---|
| The same element twice, such as `MM` and `MON` in one model | ORA-01810: format code appears twice |
| A value outside the element's range, such as month 13 | ORA-01843: not a valid month |
| `HH` (12-hour) with an hour such as 14 | ORA-01849: hour must be between 1 and 12 |
| Input that does not fit the model's punctuation under `FX` | ORA-01861: literal does not match format string |
SELECT TO_DATE('2026-13-01', 'YYYY-MM-DD') FROM dual;
-- ORA-01843: not a valid month
SELECT TO_DATE('2026-11-28 14:30', 'YYYY-MM-DD HH:MI') FROM dual;
-- ORA-01849: hour must be between 1 and 12The second example is fixed by using HH24. The chapter on conversion errors covers how to diagnose each of these in detail.
Number parsing
TO_NUMBER follows the same idea. The model must describe the layout of the input, and the digit positions must be enough for the digits provided.
SELECT TO_NUMBER('1,234.56', '9,999.99') AS parsed -- 1234.56
FROM dual;If the input has more digits than the model allows, Oracle raises ORA-01722 (invalid number). Make the model at least as wide as the largest input.
Writing good models: a checklist
- State the model explicitly in every
TO_CHARandTO_DATEcall. Do not depend onNLS_DATE_FORMAT. - Use
YYYY, notYY, when writing dates. A two-digit year loses information. - Use
HH24unless you also showAMorPM. - Use
MMfor months andMIfor minutes. Check the model aloud if it contains both. - Quote any literal word with double quotes.
- For names such as
MONTHandDAY, passNLS_DATE_LANGUAGEwhen the output language matters. - Use
FXfor strict input checks and leave it off for tolerant parsing.
Exercise
What does TO_CHAR(DATE '2026-11-28', 'HH24:MM') return, and why?
Show answer
It returns 00:11. The date literal has no time part, so the time is midnight and HH24 gives 00. The model uses MM, which is the month, so it prints 11. To show minutes, use MI, which gives 00:00.
Exercise
The call TO_DATE('2026/11/28', 'FXYYYY-MM-DD') raises ORA-01861. Give two different ways to make it succeed.
Show answer
Remove FX so that loose matching accepts the slash in place of the hyphen: TO_DATE('2026/11/28', 'YYYY-MM-DD'). Or keep FX and change the model to match the input exactly: TO_DATE('2026/11/28', 'FXYYYY/MM/DD').