Sub topic 7
Parsing strings with TO_DATE
This chapter assumes you can run SELECT statements against dual and know that a DATE value stores a date and a time. Earlier chapters covered format models and TO_CHAR. Here you use the same format models in the other direction. TO_DATE reads text and builds a DATE.
What TO_DATE does
TO_DATE converts a character string to a DATE. Its syntax is:
TO_DATE(string [, format_model [, 'NLS_DATE_LANGUAGE=language']])The format model describes how the string is laid out, element by element. Oracle reads the string from left to right and assigns each piece to the matching element. A DATE has no display format of its own. The text you see in a client comes from NLS_DATE_FORMAT or from a TO_CHAR call.
SELECT TO_DATE('28-11-2026 14:30', 'DD-MM-YYYY HH24:MI') AS parsed,
TO_CHAR(TO_DATE('28-11-2026 14:30', 'DD-MM-YYYY HH24:MI'),
'YYYY-MM-DD HH24:MI:SS') AS parsed_text
FROM dual;The first column is displayed using your session's NLS_DATE_FORMAT, which often hides the time. The second column shows the full value, 2026-11-28 14:30:00. Seconds are missing from the input, so they default to zero.
The parsing process
The diagram shows what happens when you call TO_DATE with a format model.
Errors can come from the matching step or from the validation step. The error number tells you which one failed.
Commonly used format elements
| Element | Meaning | Example input |
|---|---|---|
| `YYYY` | Four-digit year | `2026` |
| `RR` | Two-digit year with a rounding rule | `26` |
| `MM` | Month number | `11` |
| `MON` | Abbreviated month name | `NOV` |
| `MONTH` | Full month name | `NOVEMBER` |
| `DD` | Day of month | `28` |
| `HH24` | Hour, 0 to 23 | `14` |
| `HH` or `HH12` | Hour, 1 to 12, used with `AM` or `PM` | `02` |
| `MI` | Minutes | `30` |
| `SS` | Seconds | `15` |
| `AM` or `PM` | Meridian indicator | `PM` |
Month names are matched without regard to case in the string. The language used to read them comes from NLS_DATE_LANGUAGE.
Choosing the format model
Describe what the string contains, not what you want to see afterwards. The format model for '28-11-2026 14:30' is 'DD-MM-YYYY HH24:MI'. A model such as 'YYYY-MM-DD' fails on the same string because the first four characters are read as a year.
Do not leave the model out. Without it, Oracle uses the session's NLS_DATE_FORMAT. A query that works in your session can then fail in a colleague's session or in a scheduled job.
-- Depends on the session: avoid
SELECT TO_DATE('28-NOV-26') FROM dual;
-- Self-contained: prefer
SELECT TO_DATE('28-NOV-26', 'DD-MON-RR') FROM dual;For a fixed date that you type directly into code, the ANSI date literal is also an option. DATE '2026-11-28' always uses YYYY-MM-DD, whatever the session settings. TO_DATE is the right choice when the input arrives as a string, from a file, a column, or a bind variable.
Month names and language
If the string contains a month or day name, Oracle must know which language to read it in. Pass NLS_DATE_LANGUAGE as the third argument to make the call independent of the session.
SELECT TO_DATE('15-JAN-2026', 'DD-MON-YYYY', 'NLS_DATE_LANGUAGE=ENGLISH') AS parsed
FROM dual;In a session whose language is German, 'JAN' happens to work, but 'MAR' and 'DEC' do not match the German abbreviations MRZ and DEZ. The error is ORA-01843, "not a valid month". Fixing the language in the call avoids this.
Literal text in the format model
Characters such as -, /, ., : and a space are punctuation. To match other text, such as the T in an ISO 8601 timestamp, put it in double quotes inside the model.
SELECT TO_DATE('2026-11-28T14:30:00', 'YYYY-MM-DD"T"HH24:MI:SS') AS parsed
FROM dual;Without the quotes, T is not a valid format element, and Oracle raises an error.
12-hour times
Use HH or HH12 together with AM or PM. Use HH24 on its own. Mixing the two styles is an error.
SELECT TO_CHAR(TO_DATE('28-11-2026 02:30 PM', 'DD-MM-YYYY HH:MI AM'),
'YYYY-MM-DD HH24:MI') AS parsed_text
FROM dual;The result is 2026-11-28 14:30. If you used HH with the string '14:30', the hour is outside 1 to 12 and Oracle raises ORA-01849.
Defaults for missing parts
You do not have to supply every part of the date. Oracle fills in the missing ones.
| Missing part | Default |
|---|---|
| Time | Midnight (`00:00:00`) |
| Day | First day of the month |
| Month | Current month |
| Year | Current year |
SELECT TO_CHAR(TO_DATE('2026-11', 'YYYY-MM'), 'YYYY-MM-DD HH24:MI:SS') AS month_only,
TO_CHAR(TO_DATE('14:30', 'HH24:MI'), 'YYYY-MM-DD HH24:MI:SS') AS time_only
FROM dual;The first column is 2026-11-01 00:00:00. The second column has no date in its input, so it receives the first day of the current month at 14:30. That is rarely what you want. If a string has a time but no date, add the date yourself before parsing.
Strict and lenient matching
By default, Oracle is lenient when it matches the string to the model.
- Any non-alphanumeric character in the string matches any punctuation in the model.
'28/11/2026'parses with'DD-MM-YYYY'. - Leading zeros may be left out.
'1-2-2026'parses with'DD-MM-YYYY'.
The FX (format exact) modifier turns this off. The punctuation and the digit count must then match the model exactly.
SELECT TO_DATE('1-2-2026', 'DD-MM-YYYY') AS lenient
FROM dual;
-- Returns 01 February 2026
SELECT TO_DATE('1-2-2026', 'FXDD-MM-YYYY') AS strict
FROM dual;
-- ORA-01861: literal does not match format stringUse FX when you validate input from an outside source and want to reject sloppy values. Leave it off when you read files that are written inconsistently.
Pitfalls
Four-digit year with a short value
YYYY does not guess the century. The string '01-01-26' parsed with 'DD-MM-YYYY' returns the year 0026, not 2026. Use RR for two-digit years, as covered in the chapter on two-digit years.
Passing a DATE to TO_DATE
TO_DATE expects a string. If you pass a DATE, Oracle first converts it to text using NLS_DATE_FORMAT, then parses that text with your model. The result depends on the session.
-- Wrong: SYSDATE is already a DATE
SELECT TO_DATE(SYSDATE, 'DD-MM-YYYY') FROM dual;With the common default DD-MON-RR, the converted text is something like 28-NOV-26. Parsing it with DD-MM-YYYY raises ORA-01843. If you only wanted to drop the time, use TRUNC(SYSDATE).
Extra text in the input
If the string is longer than the model covers, Oracle raises ORA-01830, "date format picture ends before converting entire input string". Add the missing elements to the model or trim the string first.
Fractional seconds
A DATE stores whole seconds only. A string such as '14:30:15.123' cannot be parsed into a DATE. Use TO_TIMESTAMP with the FF element for that.
Impossible dates
Even when the string matches the model, Oracle checks that the date exists on the calendar.
| Input | Model | Error |
|---|---|---|
| `'28-13-2026'` | `DD-MM-YYYY` | ORA-01843, not a valid month |
| `'32-01-2026'` | `DD-MM-YYYY` | ORA-01847, day of month must be between 1 and the last day of the month |
| `'31-02-2026'` | `DD-MM-YYYY` | ORA-01839, date not valid for month specified |
| `'28-11-2026'` | `YYYY-MM-DD` | ORA-01843, the string is read as year 28, month 11 and day 20, and 26 is cut off or fails, depending on the settings |
The last row shows why a mismatched model produces errors that point at the wrong element. When you see ORA-01843 or ORA-01861 on text that looks fine, compare the model with the string again.
Using TO_DATE in a WHERE clause
Put TO_DATE on the constant side of a comparison, not around the column. A regular index on the column can then still be used. Because a DATE includes a time, filter with a half-open range.
SELECT *
FROM employees
WHERE hire_date >= TO_DATE('2026-11-28', 'YYYY-MM-DD')
AND hire_date < TO_DATE('2026-11-29', 'YYYY-MM-DD');This returns every row hired at any time on 28 November 2026. The condition hire_date = TO_DATE('2026-11-28', 'YYYY-MM-DD') matches only rows stored at exactly midnight.
Summary
TO_DATEreads a string using a format model and returns aDATE.- State the model and, for month names, the language. Do not rely on session settings.
- Missing parts get defaults, and a missing date gives the first day of the current month.
FXmakes matching strict. Without it, punctuation and leading zeros are forgiven.- Mismatches and impossible dates raise different ORA errors, so read the error number.
Exercise
A colleague writes SELECT TO_DATE(SYSDATE, 'DD-MM-YYYY') FROM dual; and gets ORA-01843 in a session where NLSDATEFORMAT is 'DD-MON-RR'. Explain why, and write a query that returns today's date with the time set to midnight.
Show answer
SYSDATE is already a DATE, so Oracle first converts it to text using NLSDATEFORMAT, giving something like '28-NOV-26'. TO_DATE then tries to read 'NOV' as a month number with the MM element, which fails with ORA-01843. Use SELECT TRUNC(SYSDATE) FROM dual;, which removes the time without any string conversion.
Exercise
Write a TO_DATE call that parses the string '2026-11-28T09:05:00' into a DATE, and say what 'FXDD-MM-YYYY' would do to the input '1-2-2026'.
Show answer
TO_DATE('2026-11-28T09:05:00', 'YYYY-MM-DD"T"HH24:MI:SS'). The T is a literal, so it goes in double quotes. With FX the match must be exact, and '1' has one digit where DD expects two, so Oracle raises ORA-01861 (literal does not match format string).