Skip to main content

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
ParameterDescription
quantityThe 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)
directionAn optional parameter: ago or empty string
OPTIONAdditional options when parsing interval

Supported Units and Abbreviations

UnitAbbreviations
Millennium-
Century-
Decade-
Yeary, yr, yrs
Month-
Week-
Dayd
Hourh, hr, hrs
Minutemin, mins, m
Seconds, sec, secs
Millisecondms
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 as YEAR, MONTH, DAY, HOUR, etc.
  • interval: specified timestamp.
As the output of the above query, only the minutes part will be returned.
If you query a field that is not specified in the timestamp, you will get 0 as an output.