Sub topic 3
Formatting dates with TO_CHAR
This chapter covers how TO_CHAR turns a DATE into text. It assumes you can write basic SELECT queries, know the DATE and VARCHAR2 types, and know what DUAL is. It also assumes you have read the chapter on format models. A format model is the pattern string, such as 'YYYY-MM-DD', that tells Oracle how to read or write a value.
What TO_CHAR does with a date
A DATE value in Oracle has no display format. It is stored internally as seven bytes: century, year, month, day, hour, minute, and second. When a client shows a date, something has to convert those bytes to text. TO_CHAR is the function that makes this conversion explicit.
The syntax is:
TO_CHAR(date_value [, format_model [, 'nlsparam']])If you leave out the format model, Oracle uses the session's NLS_DATE_FORMAT. That setting differs between clients, users, and databases. Code that depends on it can produce different text on different machines. Passing a format model removes that dependency.
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS now_text
FROM dual;The result is a VARCHAR2 such as 2026-11-28 07:52:43. The format model uses HH24 for a 24-hour clock, so the text is the same in every session.
The most used format elements
Each element in a format model produces one part of the output. Elements are case-insensitive for numeric parts, but the case of letter-based elements such as Day and Month controls the case of the output.
| Element | Meaning | Example output |
|---|---|---|
| `YYYY` | Four-digit year | `2026` |
| `YY` | Last two digits of the year | `26` |
| `MM` | Month number, 01 to 12 | `11` |
| `MON` | Abbreviated month name | `NOV` |
| `Month` | Full month name, padded | `November` |
| `DD` | Day of month, 01 to 31 | `28` |
| `D` | Day of week number (depends on NLS_TERRITORY) | `7` |
| `DY` | Abbreviated day name | `SAT` |
| `Day` | Full day name, padded | `Saturday` |
| `DDD` | Day of year, 001 to 366 | `332` |
| `HH24` | Hour, 00 to 23 | `07` |
| `HH` or `HH12` | Hour, 01 to 12 | `07` |
| `MI` | Minute, 00 to 59 | `52` |
| `SS` | Second, 00 to 59 | `43` |
| `AM` or `PM` | Meridian indicator | `AM` |
| `Q` | Quarter of the year | `4` |
| `WW` | Week of year, counted from January 1 | `48` |
| `IW` | ISO week number | `48` |
A few points are easy to miss:
MImeans minutes andMMmeans months. WritingHH24:MMis a common mistake. It does not raise an error. It prints the month number where the minutes should be.Dreturns a number that depends onNLS_TERRITORY. In the United States, Sunday is 1. In many European territories, Monday is 1. UseDYorDaywhen you want a name, or useTO_CHAR(d, 'D')only when you control the territory.WWandIWcount weeks differently.WWstarts week 1 on January 1 and runs in blocks of seven days.IWfollows ISO 8601, where weeks start on Monday and week 1 contains the first Thursday of the year. For reporting by week,IWis usually what people expect. Pair it withIYYY, the ISO year, because the first days of January can belong to the last ISO week of the previous year.
Controlling the case of names
The case you type in the format element decides the case of the result.
SELECT TO_CHAR(DATE '2026-11-28', 'DAY') AS upper_name,
TO_CHAR(DATE '2026-11-28', 'Day') AS initcap_name,
TO_CHAR(DATE '2026-11-28', 'day') AS lower_name
FROM dual;This returns SATURDAY, Saturday, and saturday. The same rule applies to MONTH, Month, month, and to MON, Mon, mon.
Padding and the FM modifier
Day and Month are padded with trailing blanks to the length of the longest possible name. In English, Month is padded to nine characters because September has nine letters. A short name such as May comes out as May . The Day element is padded the same way, to nine characters for Wednesday.
Numeric elements are padded with leading zeros, so DD gives 05 for the fifth day. This is often what you want in a sortable timestamp but not in a sentence.
The FM (fill mode) modifier removes this padding. It toggles on at the point where you write it and applies to the elements that follow. Writing FM again toggles it off.
SELECT TO_CHAR(DATE '2026-11-28', 'FMDay, DD Month YYYY') AS long_date
FROM dual;The result is Saturday, 28 November 2026. Without FM, the text would contain extra blanks after Saturday and after November.
FM also removes leading zeros from numeric elements. That surprises many people:
SELECT TO_CHAR(DATE '2026-03-05', 'DD/MM/YYYY') AS padded,
TO_CHAR(DATE '2026-03-05', 'FMDD/MM/YYYY') AS trimmed
FROM dual;The first column is 05/03/2026. The second is 5/3/2026. If you need FM only to trim the name padding and still want two-digit days, turn it off and on around the elements, or use a different approach. For example, 'FMDay FMDD' is not enough, because the second FM switches fill mode back off. In 'FMDay FMDD' the first FM turns it on and the second turns it off, so DD is padded again. Understanding this toggle behavior avoids a lot of trial and error.
SELECT TO_CHAR(DATE '2026-03-05', 'FMDay FMDD') AS toggled
FROM dual;Here Day is trimmed and DD keeps its leading zero, giving Thursday 05.
Adding literal text
To put your own text in the output, enclose it in double quotes inside the format model. Characters such as -, /, ,, ., ;, and : and the space are copied as they are. Other letters must be quoted, or Oracle tries to read them as format elements and raises an error.
SELECT TO_CHAR(DATE '2026-11-28', 'FM"Week" IW "of" IYYY') AS label
FROM dual;The result is Week 48 of 2026. Without the double quotes around Week and of, Oracle fails with ORA-01821: date format not recognized.
To include a literal double quote, write two double quotes in a row inside a quoted string. Remember too that the whole format model is itself a SQL string, so a single quote inside it must be doubled.
Ordinal and spelled-out forms
Two suffixes change how numbers are written:
THadds an ordinal suffix such asst,nd,rd, orth. It uses the case of the element that precedes it.SPspells out the number.SPTHcombines both.
SELECT TO_CHAR(DATE '2026-11-28', 'FMMonth DDth, YYYY') AS ordinal_form,
TO_CHAR(DATE '2026-11-28', 'FMDDSP Month') AS spelled_form
FROM dual;The first column is November 28th, 2026. The second is Twenty-Eight November. The spelled output follows the case of the element: DDSP gives TWENTY-EIGHT, Ddsp gives Twenty-Eight, and ddsp gives twenty-eight. The SP and TH suffixes are produced in English only, whatever the session language.
Language of names: nlsparam
Names such as Day and Month follow the session's NLS_DATE_LANGUAGE. A report that runs correctly in an English session can print German or French names in another session. The third argument of TO_CHAR fixes the language for one call.
SELECT TO_CHAR(DATE '2026-11-28', 'FMDay, DD Month YYYY',
'NLS_DATE_LANGUAGE=GERMAN') AS german_date,
TO_CHAR(DATE '2026-11-28', 'FMDay, DD Month YYYY',
'NLS_DATE_LANGUAGE=ENGLISH') AS english_date
FROM dual;The result is Samstag, 28 November 2026 and Saturday, 28 November 2026. When the output is meant for people, set the language explicitly. When the output is meant for another program, avoid names altogether and use numeric elements.
Time of day and the DATE type
A DATE always holds a time. If you insert DATE '2026-11-28', the time is midnight, 00:00:00. A default display that shows only the day hides this. TO_CHAR with HH24:MI:SS shows the whole value, which is useful when you suspect a column contains times you did not expect.
SELECT TO_CHAR(TRUNC(SYSDATE), 'YYYY-MM-DD HH24:MI:SS') AS midnight_today,
TO_CHAR(SYSDATE, 'HH12:MI AM') AS clock_time
FROM dual;TRUNC(SYSDATE) removes the time, so the first column ends in 00:00:00. The second column shows a 12-hour clock with AM or PM.
Two other points about the 12-hour clock: HH and HH12 both run from 01 to 12, so HH alone cannot tell 07:52 from 19:52. Add AM or PM when you use them. AM and PM are interchangeable in the model. Both print AM or PM according to the time.
Grouping with TO_CHAR
Format elements are also used to group rows. Because TO_CHAR returns text, the result sorts alphabetically, so choose the format with that in mind.
SELECT TO_CHAR(hire_date, 'YYYY-MM') AS hire_month,
COUNT(*) AS hires
FROM employees
GROUP BY TO_CHAR(hire_date, 'YYYY-MM')
ORDER BY hire_month;YYYY-MM sorts correctly as text because the year comes first and every part has a fixed width. A model such as 'Mon YYYY' would sort Apr before Jan. If you need names in the output and chronological order, sort by TO_CHAR(hire_date, 'YYYY-MM') or by TRUNC(hire_date, 'MM') and display the name separately.
This is a good use of TO_CHAR in the SELECT list and GROUP BY. In a WHERE clause, wrapping a column in TO_CHAR has a cost for index use. A later chapter covers that.
Errors and surprises
These are the problems you meet most often when formatting dates.
| Symptom | Cause | Fix |
|---|---|---|
| `ORA-01821: date format not recognized` | A letter in the model that is not a format element and is not quoted | Put literal words in double quotes |
| `ORA-01810: format code appears twice` | The same element, for example `YYYY` and `YY`, appears twice in one model | Remove the duplicate |
| Minutes show as the month number | `MM` used where `MI` was intended | Use `MI` for minutes |
| Output has extra blanks | `Day` or `Month` padding | Use `FM` |
| Names appear in another language | Session `NLS_DATE_LANGUAGE` | Pass the `nlsparam` argument |
| `###` shown in a result | Not applicable to dates, but seen when number models are too narrow | See the number formatting chapter |
| Year shows as `0026` or odd century | Date stored with a wrong century | Check the stored value with `YYYY` |
Note that on the formatting side, TO_CHAR is forgiving about duplicate elements in some cases, but ORA-01810 is still possible, so keep each element to one use per model.
How the pieces fit
The flow below shows what Oracle does when it evaluates a TO_CHAR call on a date.
Practical guidelines
- Always pass a format model. Never rely on
NLS_DATE_FORMATin code that others will run. - Use
HH24unless you also printAMorPM. - Use
FMfor text meant for people, and fixed-width output for files and sorting. - Use
YYYY-MM-DDstyle models for text that must sort or that another system reads. - Pass
NLS_DATE_LANGUAGEwhenever the output contains day or month names. - Quote literal words in the model with double quotes.
- Keep
TO_CHARout of theWHEREclause on indexed columns when you can.
Exercise
Write a query that returns the date 2026-03-05 as the text Thursday, 5th March 2026, with no extra blanks and with English names regardless of session language. Then explain why FMDay, DDth Month YYYY alone would print 05th.
Show answer
SELECT TOCHAR(DATE '2026-03-05', 'FMDay, fmDDth Month YYYY', 'NLSDATE_LANGUAGE=ENGLISH') AS txt FROM dual; Here FM at the start turns fill mode on, so Day is trimmed. The second FM (written as fm) would toggle it off, so use a single FM at the start instead: 'FMDay, DDth Month YYYY'. With one FM that stays on for the whole model, DD loses its leading zero and the output is Thursday, 5th March 2026. The claim in the question is wrong for this model: FM at the start of the model removes padding from every element after it, including the leading zero of DD, so 05th does not appear. A second FM would turn fill mode off again and bring the 05 back.
Exercise
A report groups sales by TO_CHAR(sale_date, 'Mon YYYY') and the months appear in the order Apr, Aug, Dec, Feb. What is the cause, and how do you fix it?
Show answer
TOCHAR returns text, and text sorts alphabetically, so Apr comes before Aug, Dec, and Feb. Group and sort on a model that sorts correctly, such as 'YYYY-MM', or sort on TRUNC(saledate, 'MM') and show the month name only in the displayed column.
Exercise
What is the difference between the elements WW and IW, and which one pairs with IYYY?
Show answer
WW counts blocks of seven days from January 1, so week 1 always begins on January 1. IW follows ISO 8601, where weeks start on Monday and week 1 contains the first Thursday of the year. IW pairs with IYYY, the ISO year, because the first days of January can belong to the last ISO week of the previous year.