Sub topic 7
Parsing Strings with `TO_DATE`
Assume you know basic SELECT and WHERE syntax, the DATE and VARCHAR2 data types, and how to run a query against Oracle's DUAL table. This chapter focuses on converting character strings into Oracle DATE values.
What `TO_DATE` Does
TO_DATE parses a character string and returns a DATE value. A DATE stores a calendar date and a time down to seconds. It is different from a string because Oracle can compare, sort, and perform date arithmetic on it.
Use a format model to tell Oracle how each part of the input string is arranged:
SELECT TO_DATE('28-11-2026 14:30', 'DD-MM-YYYY HH24:MI') AS parsed_date
FROM dual;The input and format model correspond element by element:
| Input part | Format element | Meaning |
|---|---|---|
| `28` | `DD` | Day of the month |
| `11` | `MM` | Month number |
| `2026` | `YYYY` | Four-digit year |
| `14` | `HH24` | Hour from 00 through 23 |
| `30` | `MI` | Minute |
The result is a DATE, even though a client might display it as text. The display format usually comes from the session's NLS_DATE_FORMAT setting. To make the result visible in a known format, format it again with TO_CHAR:
SELECT TO_CHAR(
TO_DATE('28-11-2026 14:30', 'DD-MM-YYYY HH24:MI'),
'YYYY-MM-DD HH24:MI:SS'
) AS parsed_date
FROM dual;Match the Format Model
The format model must describe the input accurately. MM means a numeric month, while MON means an abbreviated month name and MONTH means a full month name.
SELECT TO_CHAR(
TO_DATE('15-JAN-2026', 'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=ENGLISH'),
'YYYY-MM-DD'
) AS parsed_date
FROM dual;Use HH24 for a 24-hour clock. Use HH with AM or PM for a 12-hour clock:
SELECT TO_CHAR(
TO_DATE('28-11-2026 02:30 PM', 'DD-MM-YYYY HH:MI PM'),
'YYYY-MM-DD HH24:MI:SS'
) AS parsed_date
FROM dual;A mismatch can produce errors such as ORA-01861, which means a literal does not match the format string, or ORA-01843, which means the month is not recognized. Invalid calendar dates, such as 31 February, also fail.
Control Language with `NLS_DATE_LANGUAGE`
Month and day names depend on language settings unless you specify a language. The third argument to TO_DATE is an nlsparam value:
SELECT TO_CHAR(
TO_DATE('15-JAN-2026', 'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=ENGLISH'),
'YYYY-MM-DD'
) AS parsed_date
FROM dual;This makes the query independent of the session's language. Use the same approach when input contains names such as JAN, MARCH, or LUNDI.
Two-Digit Years
Prefer four-digit years with YYYY. If legacy data contains two-digit years, understand the difference between YY and RR. YY uses the current century. RR applies a century rule intended to interpret older two-digit dates more usefully.
SELECT TO_CHAR(TO_DATE('01-01-99', 'DD-MM-RR'), 'YYYY') AS rr_year,
TO_CHAR(TO_DATE('01-01-99', 'DD-MM-YY'), 'YYYY') AS yy_year
FROM dual;With the current year in 2000–2049, RR interprets 99 as 1999, while YY interprets it as 2099. This behavior is another reason to store four-digit years in new data.
Use `TO_DATE` in Filters
Convert a constant rather than converting the date column:
SELECT employee_id, hire_date
FROM employees
WHERE hire_date >= TO_DATE('2026-01-01', 'YYYY-MM-DD')
AND hire_date < TO_DATE('2027-01-01', 'YYYY-MM-DD');The half-open range includes every time on 1 January 2026 through 31 December 2026. Leaving hire_date unchanged allows a normal index on that column to remain usable.
Exercise
Which expression correctly parses 2026/11/28 14:30 into a DATE?
Show answer
TO_DATE('2026/11/28 14:30', 'YYYY/MM/DD HH24:MI')