Sub topic 13
When to use INTERVAL and TIMESTAMP instead
This chapter assumes you have read the earlier chapters on DATE, SYSDATE, ADD_MONTHS, and MONTHS_BETWEEN. You should also be comfortable with TRUNC and half-open date range filters. Here you compare those tools with Oracle's TIMESTAMP and INTERVAL types and learn when each is the better fit.
What DATE cannot do
A DATE stores a century, year, month, day, hour, minute, and second. That has three limits:
- It has no fractional seconds.
- It has no time zone.
- Subtracting two dates returns a plain number of days. The result is not a typed duration, so you must convert the fractions yourself.
TIMESTAMP and INTERVAL address these limits. They do not replace the DATE functions. Most business queries that work at day or month granularity are simpler and more portable with DATE, ADD_MONTHS, and MONTHS_BETWEEN.
The datetime and interval types
| Type | Stores | Typical use |
|---|---|---|
| `DATE` | Date and time to the second | Business dates, birthdays, order dates |
| `TIMESTAMP(p)` | Date and time with fractional seconds (`p` is 0 to 9, default 6) | Event logs, audit trails |
| `TIMESTAMP(p) WITH TIME ZONE` | A timestamp plus its original UTC offset or region | Data from several regions, where the original offset matters |
| `TIMESTAMP(p) WITH LOCAL TIME ZONE` | A timestamp normalized to the database time zone and shown in the session time zone | Instants displayed to users in their own zone |
| `INTERVAL YEAR TO MONTH` | A duration in years and months | "Three months", "one year six months" |
| `INTERVAL DAY TO SECOND` | A duration in days, hours, minutes, and seconds | "2 days 3 hours", elapsed time |
SYSTIMESTAMP returns a TIMESTAMP WITH TIME ZONE from the database server. CURRENT_TIMESTAMP does the same in the session time zone, and LOCALTIMESTAMP returns a TIMESTAMP without a zone.
Subtracting timestamps returns an interval
Subtracting two DATE values gives a number of days. Subtracting two TIMESTAMP values gives an INTERVAL DAY TO SECOND.
SELECT TIMESTAMP '2024-03-01 12:30:00'
- TIMESTAMP '2024-02-01 08:00:00' AS elapsed
FROM dual;The result is 29 days, 4 hours, and 30 minutes. February 2024 has 29 days, so the same rule holds as in the DATE examples.
You can pull parts out of an interval with EXTRACT. This avoids the multiply-and-MOD arithmetic that fractional day numbers need.
SELECT EXTRACT(DAY FROM elapsed) AS days,
EXTRACT(HOUR FROM elapsed) AS hours,
EXTRACT(MINUTE FROM elapsed) AS minutes
FROM (SELECT TIMESTAMP '2024-03-01 12:30:00'
- TIMESTAMP '2024-02-01 08:00:00' AS elapsed
FROM dual);This returns 29, 4, and 30.
Timestamps with time zones are compared as instants, not as wall-clock readings:
SELECT TIMESTAMP '2024-03-10 09:00:00 -05:00'
- TIMESTAMP '2024-03-10 09:00:00 +01:00' AS zone_gap
FROM dual;Both values show 09:00, but they are 6 hours apart. The first is 14:00 UTC and the second is 08:00 UTC.
Interval literals and conversion functions
There are two kinds of interval literal, one for each interval type.
SELECT DATE '2024-01-15' + INTERVAL '1-6' YEAR TO MONTH AS plus_18_months,
TO_CHAR(DATE '2024-01-15' + INTERVAL '2 03:30:00' DAY TO SECOND,
'YYYY-MM-DD HH24:MI:SS') AS plus_2d_3h30m
FROM dual;The first column is 2025-07-15, because '1-6' means 1 year and 6 months. The second is 2024-01-17 03:30:00.
NUMTOYMINTERVAL and NUMTODSINTERVAL turn a number into an interval. This helps when the amount comes from a column or bind variable.
SELECT DATE '2024-01-01' + NUMTODSINTERVAL(90, 'MINUTE') AS plus_90_minutes,
NUMTOYMINTERVAL(18, 'MONTH') AS eighteen_months
FROM dual;The first column is 2024-01-01 01:30:00, and the second is +01-06. NUMTODSINTERVAL(90, 'MINUTE') is the same duration as the DATE arithmetic 90/1440, but the unit is stated in the code.
Month intervals fail where ADD_MONTHS clamps
This is the most important difference for this tutorial. ADD_MONTHS moves to the last day of the target month when the day does not exist there. Adding INTERVAL ... MONTH does not do this. It raises an error when the result would be an invalid date.
-- Works: day 15 exists in February
SELECT DATE '2024-01-15' + INTERVAL '1' MONTH FROM dual;
-- Fails with ORA-01839: date not valid for month specified
SELECT DATE '2024-01-31' + INTERVAL '1' MONTH FROM dual;
-- Works: clamps to 2024-02-29
SELECT ADD_MONTHS(DATE '2024-01-31', 1) FROM dual;The first and third queries are safe for any input. The second fails only for month-end dates, so it can pass testing and then break in production on the 29th, 30th, or 31st.
Intervals from a date difference
You can ask Oracle to express a difference as an interval. Write the subtraction in parentheses, followed by the interval qualifier.
SELECT (DATE '2024-03-15' - DATE '1990-06-15') YEAR TO MONTH AS age_interval
FROM dual;This returns +33-09, which is 33 years and 9 months. For ordinary age calculations, the MONTHS_BETWEEN approach from the earlier chapter is easier to read and is the idiom most Oracle developers expect. Use the interval form when you want a typed value to store, compare, or pass to EXTRACT.
Type conversions that lose information
Several functions you have already learned return DATE, whatever type you pass in. This drops the fractional seconds and the time zone:
ADD_MONTHSreturns aDATE.TRUNCon a timestamp returns aDATE.LAST_DAYandNEXT_DAYreturn aDATE.MONTHS_BETWEENconverts timestamp arguments to dates before it measures.
SELECT TO_CHAR(ADD_MONTHS(TIMESTAMP '2024-01-15 10:00:00.123456', 1),
'YYYY-MM-DD HH24:MI:SS.FF3') AS fraction_lost
FROM dual;TO_CHAR accepts a FF3 format element because Oracle converts the DATE to a timestamp for formatting. The fractional part prints as .000, because it was discarded before the formatting step.
When a query compares a DATE with a TIMESTAMP, Oracle converts the DATE to a TIMESTAMP. The result of subtracting them is an interval, not a number of days. This surprises code that expects a number.
Range filters on timestamp columns
The half-open range pattern from the previous chapter works unchanged. A timestamp column makes it more important, because the time component is rarely midnight and has fractional seconds.
SELECT event_id, created_at
FROM events
WHERE created_at >= TRUNC(SYSTIMESTAMP) - INTERVAL '7' DAY
AND created_at < TRUNC(SYSTIMESTAMP) + INTERVAL '1' DAY;TRUNC(SYSTIMESTAMP) returns a DATE at midnight. Adding or subtracting a day interval returns a DATE, and Oracle then compares it with the timestamp column. Avoid BETWEEN ... AND TRUNC(x) + 1 - 1/86400. That form misses rows in the last second of the day, because a timestamp can be 23:59:59.5.
Choosing between the options
Use these criteria:
- Keep
DATEwhen the business meaning is a calendar day or a time to the second in one time zone. Birthdays, hire dates, and invoice dates fit this. - Choose
TIMESTAMPwhen ordering events within the same second matters, or when you measure durations shorter than a second. - Choose
TIMESTAMP WITH TIME ZONEwhen the original offset is part of the data. ChooseWITH LOCAL TIME ZONEwhen you only need to show one instant in each user's own zone. - Use
INTERVAL DAY TO SECONDfor elapsed time that you will store, display, or take apart withEXTRACT. - Use
INTERVAL YEAR TO MONTHfor durations in whole months or years, and remember the end-of-month error described above. - Prefer
ADD_MONTHSover month intervals for any user-supplied or column-based date.
Summary of the main points
- Subtracting
DATEvalues gives a number of days. SubtractingTIMESTAMPvalues gives anINTERVAL DAY TO SECOND. EXTRACTreads days, hours, and minutes from an interval without manual arithmetic.INTERVAL 'n' MONTHraises ORA-01839 when the day does not exist in the target month.ADD_MONTHSclamps to the month end.ADD_MONTHS,TRUNC,LAST_DAY, andNEXT_DAYreturn aDATE, so fractional seconds and time zones are lost.- Half-open ranges (
>=start,<next boundary) stay correct for timestamp columns.
Exercise
A query computes DATE '2024-03-31' + INTERVAL '1' MONTH and fails. What error occurs, why does it occur, and which expression returns 2024-04-30 instead?
Show answer
It raises ORA-01839, "date not valid for month specified", because April has no day 31 and interval arithmetic does not clamp. ADD_MONTHS(DATE '2024-03-31', 1) returns 2024-04-30, because it uses the last day of the target month.
Exercise
You subtract TIMESTAMP '2024-05-01 18:45:00' from TIMESTAMP '2024-05-03 06:15:00'. What type does the result have, and what do EXTRACT(DAY ...), EXTRACT(HOUR ...), and EXTRACT(MINUTE ...) return for it?
Show answer
The result is an INTERVAL DAY TO SECOND of 1 day, 11 hours, and 30 minutes. From May 1 at 18:45 to May 2 at 18:45 is one day, and from there to May 3 at 06:15 is 11 hours 30 minutes. EXTRACT returns 1, 11, and 30.
Exercise
A table has a TIMESTAMP column logged_at. Write a WHERE clause that returns all rows from the current calendar day without missing any row at 23:59:59.9.
Show answer
WHERE loggedat >= TRUNC(SYSTIMESTAMP) AND loggedat < TRUNC(SYSTIMESTAMP) + 1. The upper bound is the next midnight and is excluded, so every fractional second of the current day is included. + INTERVAL '1' DAY is equivalent.