Sub topic 1
What an Oracle DATE really stores
Prerequisites
This chapter assumes you can:
- Write basic
SELECTqueries with aWHEREclause. - Use the
DUALtable for one-row queries. - Tell numeric, character, and date columns apart.
You need a schema where you can create a small table. Everything else is explained here.
The DATE type is a date and a time
An Oracle DATE stores a calendar date and a time of day. Many people assume it holds only a date because tools often print it that way. Every DATE value has seven components:
- century
- year
- month
- day
- hour
- minute
- second
The type has no fractions of a second and no time zone. Valid values run from January 1, 4712 BC to December 31, 9999 AD.
This matters for every later chapter. Date arithmetic, TRUNC, ADD_MONTHS, and MONTHS_BETWEEN all operate on the full value, time included. A query that ignores the time portion can return wrong rows without raising an error.
How the value is stored internally
Oracle stores a DATE column value in 7 bytes, one byte per component. Some components are offset so that no byte is zero:
| Byte | Component | Stored as |
|---|---|---|
| 1 | Century | century + 100 |
| 2 | Year within century | year + 100 |
| 3 | Month | 1 to 12 |
| 4 | Day | 1 to 31 |
| 5 | Hour | hour + 1 |
| 6 | Minute | minute + 1 |
| 7 | Second | second + 1 |
You can see this with the DUMP function. The following script creates a table and stores 10:30:45 on March 15, 2024.
CREATE TABLE date_demo (
id NUMBER PRIMARY KEY,
happened_at DATE
);
INSERT INTO date_demo (id, happened_at)
VALUES (1, TO_DATE('2024-03-15 10:30:45', 'YYYY-MM-DD HH24:MI:SS'));
COMMIT;
SELECT TO_CHAR(happened_at, 'YYYY-MM-DD HH24:MI:SS') AS shown,
DUMP(happened_at) AS raw_bytes
FROM date_demo
WHERE id = 1;The raw_bytes column shows Typ=12 Len=7: 120,124,3,15,11,31,46. Reading the bytes in order:
120is century 20 plus 100.124is year 24 plus 100.3and15are the month and day.11is hour 10 plus 1.31is minute 30 plus 1.46is second 45 plus 1.
You will never need to decode these bytes in daily work. The point is that the time is always present in the stored value.
Midnight is the default time
If you give Oracle a date without a time, it fills in 00:00:00. This happens in three common cases:
- A
DATE '2024-03-15'literal. The literal syntax accepts onlyYYYY-MM-DD, so it can never carry a time. - A
TO_DATEcall whose format mask has no time elements. - An implicit conversion from a string that has no time part.
The following query shows both cases.
SELECT TO_CHAR(DATE '2024-03-15', 'YYYY-MM-DD HH24:MI:SS') AS literal_value,
TO_CHAR(TO_DATE('15-MAR-2024', 'DD-MON-YYYY'),
'YYYY-MM-DD HH24:MI:SS') AS to_date_value
FROM dual;Both columns show 2024-03-15 00:00:00.
The reverse also holds. A value displayed as just a date may still contain a time. Whether you see the time depends only on the display format, not on what is stored. Formatting is covered in more detail in the next chapter, but one point is needed now. Your session's NLS_DATE_FORMAT setting controls how a DATE is converted to text when you do not call TO_CHAR. A common default is DD-MON-YY (or DD-MON-RR), which hides the time entirely. Your client tool may override this setting, so output varies between environments.
The midnight trap
The most common bug with DATE is an equality comparison on a column that holds times. Add a second row to the demo table. One row is at midnight and the other is in the afternoon of the same day.
INSERT INTO date_demo (id, happened_at)
VALUES (2, DATE '2024-03-15');
INSERT INTO date_demo (id, happened_at)
VALUES (3, TO_DATE('2024-03-15 14:45:00', 'YYYY-MM-DD HH24:MI:SS'));
COMMIT;
-- Wrong if you want everything on March 15: matches only rows at exactly 00:00:00
SELECT id
FROM date_demo
WHERE happened_at = DATE '2024-03-15';
-- Right: a half-open range covering the whole day
SELECT id
FROM date_demo
WHERE happened_at >= DATE '2024-03-15'
AND happened_at < DATE '2024-03-16'
ORDER BY id;The first query returns only row 2. The second returns rows 1, 2, and 3, because it covers midnight to just before the next midnight.
The half-open pattern (>= start, < next start) is the safest way to filter a day or any other period. It works whatever times are stored, and it avoids guessing a last moment such as 23:59:59. Later chapters apply it to rolling windows with TRUNC and ADD_MONTHS.
You may be tempted to write TRUNC(happened_at) = DATE '2024-03-15'. This returns the right rows, but applying a function to the column normally prevents Oracle from using a plain index on happened_at. The range form keeps the column untouched, so an index can be used.
DATE compared with TIMESTAMP
Oracle also offers TIMESTAMP, which has the same calendar and time components plus fractional seconds. The main differences are:
| Feature | DATE | TIMESTAMP |
|---|---|---|
| Storage size | 7 bytes | 7 to 11 bytes |
| Fractional seconds | None | Up to 9 digits, default precision 6 |
| Time zone | None | Only in `TIMESTAMP WITH TIME ZONE` and `WITH LOCAL TIME ZONE` variants |
| Result of subtraction | A number of days | An `INTERVAL DAY TO SECOND` |
| Literal syntax | `DATE 'YYYY-MM-DD'` | `TIMESTAMP 'YYYY-MM-DD HH24:MI:SS.FF'` |
The subtraction difference is the one that most often surprises people. Run this query.
SELECT DATE '2024-03-16' - DATE '2024-03-15' AS date_difference,
TIMESTAMP '2024-03-16 12:00:00'
- TIMESTAMP '2024-03-15 00:00:00' AS timestamp_difference
FROM dual;date_difference is the number 1. timestamp_difference is an interval, shown as +000000001 12:00:00.000000. The two results need different handling in later code. This tutorial focuses on DATE, because ADD_MONTHS and MONTHS_BETWEEN are defined for dates, and a final chapter returns to when INTERVAL and TIMESTAMP are the better choice.
If you pass a TIMESTAMP to ADD_MONTHS, Oracle converts it implicitly to DATE and returns a DATE. The fractional seconds are lost. Check the result type if you rely on them.
Seeing the current moment
SYSDATE returns the current date and time of the database server as a DATE, so it has a nonzero time portion except at exactly midnight. Compare it with its relatives:
SELECT SYSDATE,
SYSTIMESTAMP,
CURRENT_DATE,
TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS formatted_now
FROM dual;SYSDATEis aDATEin the database server's operating system time.SYSTIMESTAMPis aTIMESTAMP WITH TIME ZONEwith fractional seconds, also from the server.CURRENT_DATEis aDATEin the session's time zone.
The first two columns may look identical to a plain date in some tools, since the default display hides the time. The formatted_now column always shows it. The next chapters cover these functions in detail, so for now remember that SYSDATE carries a time like any other DATE.
Common errors
- Equality on a column with times.
WHERE d = DATE '...'silently misses rows that are not at midnight. Use a half-open range. - Trusting the default display. A column may look like it holds plain dates when it holds times. Check with
TO_CHARand a full format. - Comparing a date to a string.
WHERE d = '15-MAR-24'forces Oracle to convert the string usingNLS_DATE_FORMAT. The query works in one session and fails or misreads in another. Use aDATEliteral orTO_DATEwith an explicit mask. - Two-digit years. A
YYorRRformat guesses the century. UseYYYYin masks you control. - Expecting fractions of a second. A
DATEcannot store them. UseTIMESTAMPwhen sub-second precision matters.
Key points
- A
DATEis a date plus a time of day, to the second, in 7 bytes. - It has no fractional seconds and no time zone.
- A missing time becomes
00:00:00. - Display format controls what you see, not what is stored.
- Filter on a day with a half-open range instead of
=.
Exercise
A table shipments has a DATE column shipped_at, and some rows were stored with times such as 16:20:00. A colleague writes WHERE shipped_at = DATE '2024-05-10' to find all shipments on May 10, 2024, and reports that rows are missing. Explain why, and write a query that returns every shipment on that day without applying a function to the column.
Show answer
A DATE literal has the time 00:00:00, and the equality test matches only rows stored at exactly midnight on May 10. Rows such as 2024-05-10 16:20:00 are not equal to it, so they are skipped. The fix is a half-open range: SELECT * FROM shipments WHERE shipped_at >= DATE '2024-05-10' AND shipped_at < DATE '2024-05-11';. This covers every time on May 10, and leaving the column untouched lets Oracle use an index on shipped_at.
Exercise
What do DATE '2024-03-16' - DATE '2024-03-15' and TIMESTAMP '2024-03-16 00:00:00' - TIMESTAMP '2024-03-15 00:00:00' return, and why do the results differ in type?
Show answer
The DATE subtraction returns the number 1, a count of days. The TIMESTAMP subtraction returns an INTERVAL DAY TO SECOND value, shown as +000000001 00:00:00.000000. Oracle defines subtraction of two DATE values to yield a number of days, with fractions for the time. It defines subtraction of two timestamps to yield an interval, which can also carry fractional seconds.