interval
The Interval data type represents periods between dates or times, which can be precisely calculated and expressed through various units. Those can be combined and include additional options for different interval calculations.
Syntax
The syntax for specifying an interval is as follows:
SELECT INTERVAL 'quantity unit [quantity unit...] [direction]' [OPTION]
| Parameter | Description |
|---|---|
|
The value representing the number of units. |
|
Year, month, day, hour, and minute. Abbreviations, short forms, and dash format are supported. Plural forms are also acceptable (for example, months, days, weeks). |
|
An optional parameter: |
|
Additional options when parsing interval. |
|
For arithmetic and comparison operations, Redpanda SQL assumes |
Supported units and abbreviations
| Unit | Abbreviations |
|---|---|
Millennium |
- |
Century |
- |
Decade |
- |
Year |
|
Month |
- |
Week |
- |
Day |
|
Hour |
|
Minute |
|
Second |
|
Millisecond |
|
Microsecond |
- |
Options for interval parsing
-
YEAR,MONTH,DAY,HOUR,MINUTE,SECOND -
YEAR TO MONTH,DAY TO HOUR,DAY TO MINUTE,DAY TO SECOND,HOUR TO MINUTE,HOUR TO SECOND,MINUTE TO SECOND
Examples
Select interval with multiple units
This example calculates an interval by combining multiple units of time.
SELECT INTERVAL '5 years 4 months 2 weeks 3 days 5 hours 10 minutes 25 seconds' as "Interval";
Interval
---------------------------------
5 years 4 mons 17 days 05:10:25
(1 row)
Use abbreviations
This example shows how to use abbreviated units for time intervals.
SELECT INTERVAL '10 yr 8 months 2 weeks 6 days 5 hrs 10 min 20 s' as "Interval";
Interval
----------------------------------
10 years 8 mons 20 days 05:10:20
(1 row)
Use dash format
SELECT INTERVAL '1-2 3 DAYS 04:05:06.070809' as "Interval";
Interval
--------------------------------------
1 year 2 mons 3 days 04:05:06.070809
(1 row)
Parse intervals using specific units
By running the following code, the output shows everything up to minutes and ignores seconds and milliseconds.
SELECT INTERVAL '1-2 5 DAYS 07:08:06.040809' MINUTE as "Interval";
Interval
-------------------------------
1 year 2 mons 5 days 07:08:00
(1 row)
Display specific range only
Executing the following query results in only years and months being displayed, excluding days, hours, minutes, and seconds from the input.
SELECT INTERVAL '2-4 5 DAYS 04:05:06.070809' YEAR TO MONTH as "Interval";
Interval
----------------
2 years 4 mons
(1 row)
Extract data from interval
To extract interval numbers from a timestamp, use the EXTRACT function:
SELECT EXTRACT (field FROM interval)
-
field: Supports time units, such asYEAR,MONTH,DAY, andHOUR. -
interval: Specified timestamp.
SELECT EXTRACT (MINUTE
FROM INTERVAL '2 hours 30 minutes');
The output returns only the minutes part:
extract
------------
30
(1 row)
|
If you query a field that is not specified in the timestamp, the output is |