The TO_DATE function is one of the most essential and widely used functions in Oracle SQL. Its one and only job is to convert a text string (like '2025-01-30' or '15-Jan-2025') into an actual DATE data type that Oracle can understand and use for date-based calculations.
This function is the direct opposite of TO_CHAR(date). It is the standard way to insert or compare a string value with a DATE column.
What is the TO_DATE Function in Oracle?
The TO_DATE function "translates" a character string into a DATE. To do this, you must provide the string and a "format model" (a fmt string) that tells Oracle exactly how to read the string.
This is critical because the string '05-01-2025' could mean May 1st or January 5th, depending on the format. You use the format model to remove all guesswork.
A powerful new feature is the DEFAULT ... ON CONVERSION ERROR clause, which allows you to prevent your query from stopping with an error if the text cannot be converted.
TO_DATE Function Syntax
The syntax for TO_DATE is:
TO_DATE(char [DEFAULT return_value ON CONVERSION ERROR]
[, fmt]
[, 'nlsparam'])
Let's break that down:
char: The text string you want to convert (e.g.,'15-Jan-2025').DEFAULT return_value ON CONVERSION ERROR(Optional): This is a modern, safe feature. It provides a fallback value (likeNULLor another date) to return if thecharstring doesn't match the format, preventing the query from failing.[fmt](Optional, but Highly Recommended): The "format model" or "blueprint" that tells Oracle how to read thecharstring. For example,'DD-MON-YYYY'.[nlsparam](Optional): A string that specifies the language for month or day names (e.g.,'NLS_DATE_LANGUAGE = American').
Why You Must Specify a Format (fmt)
If you omit the fmt string, TO_DATE will use your session's default date format (NLS_DATE_FORMAT). This is dangerous. Your query might work for you, but it will break for another user in a different region who has a different default format.
Best Practice: Always specify the format model (fmt) to ensure your SQL is predictable and works for everyone.
Oracle TO_DATE Function Examples
Here are two practical examples of how to use TO_DATE.
Example 1: Basic String-to-Date Conversion using TO_DATE
This is the most common use case. We want to convert a detailed string into a DATE value. We must provide a format model (fmt) that exactly matches the string.
'Month'matches 'January''dd'matches '15''YYYY'matches '1989''HH:MI A.M.'matches '11:00 A.M.'
Query:
SELECT
TO_DATE(
'January 15, 1989, 11:00 A.M.',
'Month dd, YYYY, HH:MI A.M.',
'NLS_DATE_LANGUAGE = American'
) AS "Converted_Date"
FROM DUAL;
Result: (The output will be a DATE value, displayed in your session's default format)
Converted_Date
--------------
15-JAN-89
Example 2: Handling Conversion Errors with TO_DATE
This example uses the modern DEFAULT ... ON CONVERSION ERROR clause. The string 'Febuary 15...' is misspelled and would normally cause an ORA-01843: not a valid month error, stopping the query.
With the DEFAULT clause, the query won't stop; it will simply return the specified default date instead.
Query:
SELECT
TO_DATE(
'Febuary 15, 2016, 11:00 A.M.' DEFAULT 'January 01, 2016 12:00 A.M.' ON CONVERSION ERROR,
'Month dd, YYYY, HH:MI A.M.',
'NLS_DATE_LANGUAGE = American'
) AS "Value"
FROM DUAL;
Result: (Instead of an error, Oracle returns the default value, converted to a DATE.)
Value
---------
01-JAN-16
