Sub topic 13
When to use INTERVAL and TIMESTAMP instead
This chapter assumes that you can write basic SELECT and WHERE clauses, use the DUAL table, and distinguish numeric, character, and date values. The focus here is choosing between Oracle DATE, TIMESTAMP, and INTERVAL values.
DATE, TIMESTAMP, and INTERVAL
Oracle DATE stores a calendar date and a time down to whole seconds. It does not store fractional seconds or a time zone. TIMESTAMP stores the same date and time with fractional seconds, such as milliseconds or microseconds.
An INTERVAL is a duration. Oracle provides two main interval types:
INTERVAL YEAR TO MONTHstores calendar months and years.INTERVAL DAY TO SECONDstores days, hours, minutes, seconds, and fractional seconds.
These types describe different ideas. A timestamp is a point in time. An interval is an amount of time. A date is a point in time with less precision than a timestamp.
| Type | Stores | Typical use |
|---|---|---|
| `DATE` | Date and whole seconds | Existing applications and ordinary business dates |
| `TIMESTAMP` | Date, time, and fractional seconds | Events requiring precise timestamps |
| `INTERVAL` | A duration | Adding a month, measuring hours, or storing elapsed time |
flowchart TD
A[Need a point in time] --> B{Need fractional seconds or time zone?}
B -->|No| C[Use DATE]
B -->|Yes| D[Use TIMESTAMP variant]
E[Need a duration] --> F{Calendar months or clock time?}
F -->|Months or years| G[INTERVAL YEAR TO MONTH]
F -->|Days to fractional seconds| H[INTERVAL DAY TO SECOND]Use TIMESTAMP for precision
Use TIMESTAMP when two events can occur within the same second and must remain distinguishable.
SELECT TIMESTAMP '2024-06-15 10:30:00.123456' AS precise_event_time,
TIMESTAMP '2024-06-15 10:30:00.123456'
+ INTERVAL '250' MILLISECOND AS later_event
FROM dual;A plain TIMESTAMP does not include a time zone. Use TIMESTAMP WITH TIME ZONE when the stored offset or region matters. This is useful for appointments, messages, or transactions that cross time zones.
Use TIMESTAMP WITH LOCAL TIME ZONE when Oracle should normalize values for storage and display them in each session's time zone. This can simplify global applications, but the displayed value depends on the session settings.
Use INTERVAL for explicit durations
Date arithmetic with a number can be convenient:
SELECT SYSDATE + 1 AS tomorrow,
SYSDATE + 1/24 AS one_hour_later
FROM dual;The number represents days, so 1/24 means one hour. Intervals make the unit explicit and work naturally with timestamps:
SELECT TIMESTAMP '2024-06-15 10:30:00'
+ INTERVAL '90' MINUTE AS later_time,
TIMESTAMP '2024-06-15 10:30:00'
+ INTERVAL '2' MONTH AS later_month
FROM dual;Use INTERVAL DAY TO SECOND for clock-based durations. Use INTERVAL YEAR TO MONTH when the duration is expressed in calendar months or years.
Month-end behavior is a key difference
ADD_MONTHS returns a DATE and adjusts invalid target days to the last day of the target month:
SELECT ADD_MONTHS(DATE '2024-01-31', 1) AS adjusted_date
FROM dual;The result is February 29, 2024. Adding a year-month interval to a timestamp can instead fail when the resulting calendar date does not exist, such as February 29 plus one year. Therefore, choose ADD_MONTHS when month-end clamping is part of the business rule. Choose an interval when you need timestamp precision and the calendar operation is valid for every input.
> warning > Do not replace ADD_MONTHS with a year-month interval without testing end-of-month dates. The two operations can have different results or error behavior.
Choose based on the operation
Keep using DATE, SYSDATE, ADD_MONTHS, and MONTHS_BETWEEN for established date-based calculations. Choose TIMESTAMP when fractional seconds matter. Choose a time-zone-aware timestamp when the original geographic time matters. Choose INTERVAL when the duration and its unit should be explicit.
Exercise
Which choice is best for recording two application events that may occur 500 microseconds apart, while preserving the user's time-zone offset?
Show answer
Use TIMESTAMP WITH TIME ZONE. It preserves fractional seconds and the offset associated with the recorded time.