Sub topic 2
Writing and Displaying Dates Safely
Prerequisites and the core problem
This chapter assumes that you can write basic SELECT queries, use WHERE, and understand the DUAL table. Oracle's DATE data type stores a calendar date and a time down to seconds. It does not store a display format.
The same DATE value can appear differently depending on the session's NLS_DATE_FORMAT setting. For example, a value might display as 15-JAN-24 in one session and 2024-01-15 in another. The stored value has not changed.
Use date literals for fixed dates
For a fixed calendar date, use an ANSI date literal:
SELECT DATE '2024-01-15' AS start_date
FROM dual;The format is always YYYY-MM-DD. A date literal represents midnight at the beginning of that date. Therefore, DATE '2024-01-15' means 2024-01-15 00:00:00.
Date literals are safer than putting a formatted string directly in a query. They do not depend on the session's language or NLS_DATE_FORMAT.
Convert text with `TO_DATE`
Use TO_DATE when the input is character data. Give Oracle a format model that describes the input exactly:
SELECT TO_DATE('15/01/2024', 'DD/MM/YYYY') AS order_date
FROM dual;The first argument is the text. The second argument tells Oracle how to interpret that text.
Format models are case-insensitive for numeric elements, but separators and punctuation should match the input. Common elements include:
| Element | Meaning | Example |
|---|---|---|
| `YYYY` | Four-digit year | `2024` |
| `MM` | Two-digit month | `01` |
| `DD` | Day of month | `15` |
| `HH24` | Hour from 00 through 23 | `17` |
| `MI` | Minute | `30` |
| `SS` | Second | `45` |
For a date and time, include the time elements:
SELECT TO_DATE(
'2024-01-15 17:30:45',
'YYYY-MM-DD HH24:MI:SS'
) AS event_time
FROM dual;Avoid relying on implicit conversion, where Oracle silently converts a string to a date:
WHERE order_date = '15-JAN-24'This query may work in one session and fail or mean something different in another. It can also produce conversion errors when the input does not match the session settings.
Display dates with `TO_CHAR`
Use TO_CHAR to turn a date into text with a predictable appearance:
SELECT TO_CHAR(
DATE '2024-01-15',
'YYYY-MM-DD HH24:MI:SS'
) AS displayed_date
FROM dual;TO_CHAR is for presentation, exports, and text reports. Keep the column as a DATE when you need to compare it, sort it, or perform date arithmetic. Converting a date to text too early can make later date operations harder and can lead to incorrect string comparisons.
The following query shows both the stored date value and an explicitly formatted version:
SELECT order_date,
TO_CHAR(order_date, 'YYYY-MM-DD HH24:MI:SS') AS order_date_text
FROM orders;The first column is still a DATE, although the client controls how it initially displays. The second column is character data with the exact requested format.
Understand the conversion path
A safe conversion path keeps dates as dates until the boundary where text is required:
A value such as DATE '2024-02-01' can be compared directly with a date column. A value such as '2024-02-01' is text and should be converted explicitly before comparison.
Remember that TO_CHAR does not change the date stored in a table. It creates a formatted character result for that query.
Exercise
Which expression safely converts the text 03/07/2024 09:15 into an Oracle DATE, and which expression formats a DATE as 2024-07-03?
Show answer
Use TO_DATE('03/07/2024 09:15', 'DD/MM/YYYY HH24:MI') for the input text. Use TO_CHAR(date_value, 'YYYY-MM-DD') for the output format.