Interval Type
Overview
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.
In this doc, you’ll find more about the interval syntax, learn what are supported units and abbreviations, browse through examples and finally find out how to extract data from intervals.
Syntax
The syntax for specifying an interval is as follows:
Parameters Description
Parameter | Description |
---|---|
quantity | The value representing the number of units |
unit | - Year, month, day, hour, minute, etc. - Abbreviations, short forms and dash format is supported - Plural forms are also acceptable (e.g. months, days, weeks) |
direction | An optional parameter: ago or empty string |
OPTION | Additional options when parsing interval |
Supported Units and Abbreviations
Unit | Abbreviations |
---|---|
Millennium | - |
Century | - |
Decade | - |
Year | y , yr , yrs |
Month | - |
Week | - |
Day | d |
Hour | h , hr , hrs |
Minute | min , mins , m |
Second | s , sec , secs |
Millisecond | ms |
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
In this example, we’ll calculate the interval by combining multiple units of time.
Using Abbreviations
This example shows how to use abbreviated units for time intervals.
Using Dash Format
Here you’ll find out how to use the dash format for specifying intervals.
Parsing Intervals Using Specific Units
By running the code below, the output will show everything up to minutes and ignore seconds and miliseconds.
Displaying Specific Range Only
Executing the query below will result only years and months being displayed excluding days, hours, minutes, and seconds from the input.
Extracting Data From Interval
In order to extract the interval numbers from the timestamp, you can use the EXTRACT() function the following way:
field
: supports time units, such asYEAR
,MONTH
,DAY
,HOUR
, etc.interval
: specified timestamp.
As the output of the above query, only the minutes part will be returned.
0
as an output.