SQL not only supports date and timestamp data types but also time intervals.
An interval in SQL represents a period of time, such as days, hours, minutes, or seconds. SQL supports intervals in two main types: INTERVAL YEAR TO MONTH and INTERVAL DAY TO SECOND.
The two types
YEAR TO MONTH is used to represent a span of years and months. Examples include calculating ages, durations of contracts, or project timelines.
DAY TO SECOND is used to represent a span of days, hours, minutes, and seconds. Examples include scheduling events, logging activities, and tracking precise durations.
-- Represents 1 year and 6 months
INTERVAL '1-6' YEAR TO MONTH
-- Represents 5 days, 12 hours, 30 minutes, and 45 seconds
INTERVAL '5 12:30:45' DAY TO SECOND
This distinction may seem arbitrary at first, but it aligns with real-world use cases. An interval of “2 months” is clearly different from “60 days,” even though they might seem similar. The former is handled as a “YEAR TO MONTH” interval, and the latter as a “DAY TO SECOND” interval, making it clear how operations should be performed. Storing only the relevant units also reduces the amount of space required. For instance, storing a “YEAR TO MONTH” interval doesn’t need to allocate space for hours or seconds, thus saving storage.
Despite its name a “DAY TO SECOND” interval can also include fractional seconds:
INTERVAL '1 12:30:45.123' DAY TO SECOND
There are also shorter forms of interval literals if you only want to use one or several units of the interval:
For “YEAR TO MONTH”:
INTERVAL '3' YEAR -- 3 years
INTERVAL '5' MONTH -- 5 months
For “DAY TO SECOND”:
INTERVAL '2' DAY -- 2 days
INTERVAL '5:30' HOUR TO MINUTE -- 5 hours and 30 minutes
INTERVAL '15' MINUTE -- 15 minutes
INTERVAL '30' SECOND -- 30 seconds
INTERVAL '45.678' SECOND -- 45.678 seconds
Note that the syntax always uses the singular forms of the unit words.
The two types can be used as data types for table columns:
CREATE TABLE interval_examples (
id NUMBER,
a INTERVAL YEAR TO MONTH,
b INTERVAL DAY TO SECOND
);
Calculations with intervals
Intervals can be added to or subtracted from dates to calculate future or past dates.
-- Adding 10 days to the current date
CURRENT_DATE + INTERVAL '10' DAY
-- Subtracting 3 months from a specific date
DATE '2024-06-14' - INTERVAL '3' MONTH
It’s important to understand that intervals of type YEAR TO MONTH do not have a fixed duration. The number of days added to CURRENT_TIMESTAMP when you add an INTERVAL ‘1’ MONTH depends on the current month and year, as different months have different numbers of days.
Intervals can also be compared to each other with the usual operators such as =, <, <=, >, >=, <>.
Combining both types
Suppose you need to add an interval of 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds to the current timestamp. In this case you need to combine intervals of the two types:
SELECT (CURRENT_TIMESTAMP + INTERVAL '1-2' YEAR TO MONTH) + INTERVAL '3 04:05:06' DAY TO SECOND AS result;
Notes on PostgreSQL
PostgreSQL supports the above standard interval syntax, but it also supports a unified syntax for intervals with the following syntax:
INTERVAL '1 year 2 months'
INTERVAL '10 days 12 hours 30 minutes 45.123 seconds'
INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'
In fact, in PostgreSQL, YEAR TO MONTH and DAY TO SECOND are not a distinct data type on their own; they are specializations of the broader INTERVAL data type.
It also supports multiplication and division of intervals:
-- Results in an interval of 6 days
SELECT INTERVAL '2 days' * 3 AS multiplied_interval;
-- Results in an interval of 2 hours
SELECT INTERVAL '3 hours' / 1.5 AS divided_interval;