Sub topic 3
Formatting Dates with `TO_CHAR`
Prerequisites and the working model
This chapter assumes that you can write basic SELECT statements and that you know the DATE data type. It also assumes that you can run SQL in SQL*Plus, SQL Developer, or a similar Oracle client.
TO_CHAR converts a date or timestamp into a VARCHAR2 string. The date value stays unchanged. Only its displayed representation changes.
A date does not have a display format stored inside it. Oracle stores date information, such as the year, month, day, hour, minute, and second. Your client chooses how to display that value unless you specify a format yourself.
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS now_text
FROM dual;The format model tells Oracle which parts to include and how to arrange them:
YYYYmeans a four-digit year.MMmeans a two-digit month.DDmeans a two-digit day of the month.HH24means the hour from 00 through 23.MImeans minutes.SSmeans seconds.
The result is text, so you can display it, export it, or combine it with other text. Do not use the result for date arithmetic or date comparisons.
Reading a date format model
A format model is a string made from format elements and ordinary punctuation. Oracle reads each element and replaces it with part of the date.
SELECT TO_CHAR(DATE '2026-11-28', 'YYYY/MM/DD') AS slash_date,
TO_CHAR(DATE '2026-11-28', 'DD Mon YYYY') AS short_date,
TO_CHAR(DATE '2026-11-28', 'Day, DD Month YYYY') AS long_date
FROM dual;Possible results include:
2026/11/2828 Nov 2026Saturday, 28 November 2026
Some elements produce padded output. For example, Day and Month usually add spaces so that values have a consistent width. This can be surprising when you compare the result with another string or build a label.
Removing padding with `FM`
FM means fill mode. It suppresses padding blanks and removes unnecessary leading zeroes in relevant numeric date elements.
SELECT TO_CHAR(DATE '2026-11-28', 'Day, DD Month YYYY') AS padded_text,
TO_CHAR(DATE '2026-11-28', 'FMDay, DD Month YYYY') AS trimmed_text
FROM dual;The second expression returns text such as Saturday, 28 November 2026 without the spaces that Day and Month may add.
Use FM when the result is intended for a report label, message, file, or application interface. Keep padding when fixed-width output is useful.
Names, language, and session settings
Month and day names depend on Oracle's national language settings. A query can therefore produce different text in different sessions. The third argument to TO_CHAR sets the language for the conversion.
SELECT TO_CHAR(
DATE '2026-11-28',
'FMDay, DD Month YYYY',
'NLS_DATE_LANGUAGE=ENGLISH'
) AS stable_date_text
FROM dual;This returns English names even if the session's normal language is different. Use this argument when generated text must be consistent across users, servers, or reports.
A useful pattern
Choose a format based on where the result will go:
- Use
YYYY-MM-DDfor sortable machine-readable text. - Add
HH24:MI:SSwhen the time matters. - Use
FMDay, DD Month YYYYfor readable labels. - Set
NLS_DATE_LANGUAGEwhen names must be predictable.
Exercise
Which expression returns an English date in the form Saturday, 28 November 2026 without padding: TOCHAR(DATE '2026-11-28', 'Day, DD Month YYYY') or TOCHAR(DATE '2026-11-28', 'FMDay, DD Month YYYY', 'NLSDATELANGUAGE=ENGLISH')?
Show answer
The second expression. FM removes padding, and the NLS argument fixes the language.