Sub topic 1
What an Oracle DATE really stores
Prerequisites
Assume that you can write basic SELECT queries, use a WHERE clause, and query Oracle's DUAL table. You should also know that numeric values and character values have different data types.
The DATE data type
In Oracle, DATE stores a calendar date and a time of day. Its values include:
- Century and year
- Month
- Day
- Hour
- Minute
- Second
An Oracle DATE does not store fractional seconds or a time-zone offset. Internally, Oracle stores a DATE in seven bytes, but you normally work with it through SQL functions and operators rather than its internal representation.
The time portion is present even when a query displays only a date. For example, these values are different:
DATE '2026-11-28' -- 00:00:00
TO_DATE('2026-11-28 14:30:45',
'YYYY-MM-DD HH24:MI:SS') -- 14:30:45The first value is a date literal. Oracle assigns it midnight as its time.
DATE is not just a date
A column named created_date might contain both the creation date and the creation time. If you compare it with a date literal, the literal has a time of midnight:
SELECT CASE
WHEN TO_DATE('2026-11-28 14:30:00',
'YYYY-MM-DD HH24:MI:SS') >
DATE '2026-11-28'
THEN 'Later than midnight'
ELSE 'Equal to midnight'
END AS comparison_result
FROM dual;The result is Later than midnight. Both values refer to the same calendar day, but their times differ.
This distinction matters in filters. A condition such as event_date = DATE '2026-11-28' matches only rows at exactly midnight. It does not match events later that day. A later chapter will use half-open ranges to select a whole day safely.
DATE compared with TIMESTAMP
TIMESTAMP is similar to DATE, but it can store fractional seconds. For example, a timestamp can distinguish 10:15:20.123 from 10:15:20.987.
| Type | Date and time | Fractional seconds | Time-zone information |
|---|---|---|---|
| `DATE` | Yes, through seconds | No | No |
| `TIMESTAMP` | Yes, through seconds | Yes | No |
| `TIMESTAMP WITH TIME ZONE` | Yes | Yes | Yes |
Choose DATE when second-level precision is sufficient and the value does not need time-zone information. Choose TIMESTAMP for measurements, event ordering, or systems that require subsecond precision. Use a time-zone-aware type when the stored instant must retain its relationship to a time zone.
Display does not determine storage
Oracle converts a DATE to text for display using session settings, often the NLS_DATE_FORMAT setting. Two sessions can display the same stored value differently. A display such as 28-NOV-26 may hide the stored time completely.
Use TO_CHAR when you need a predictable display format:
SELECT TO_CHAR(
TO_DATE('2026-11-28 14:30:45', 'YYYY-MM-DD HH24:MI:SS'),
'YYYY-MM-DD HH24:MI:SS'
) AS displayed_value
FROM dual;Use TO_DATE with an explicit format when converting known text into a DATE. Do not rely on the session's default format. Date literals such as DATE '2026-11-28' are safer when the input has no time component.
Exercise
Exercise
Why might WHERE created_at = DATE '2026-11-28' fail to find a row created at 09:00 on that day?
Show answer
The date literal represents 00:00:00, while the row contains 09:00:00. The values have different time components.