INTERVAL

The INTERVAL data type stores a value that represents a span of time.

Aliases

There are no aliases for the interval type. However, CockroachDB supports using uninterpreted string literals in contexts where an INTERVAL value is otherwise expected.

Syntax

A constant value of type INTERVAL can be expressed using aninterpreted literal, or astring literalannotated withtype INTERVAL orcoerced to typeINTERVAL.

INTERVAL constants can be expressed using the following formats:

FormatDescription
SQL StandardINTERVAL 'Y-M D H:M:S'Y-M D: Using a single value defines days only; using two values defines years and months. Values must be integers.H:M:S: Using a single value defines seconds only; using two values defines hours and minutes. Values can be integers or floats.Note that each side is optional.
ISO 8601INTERVAL 'P1Y2M3DT4H5M6S'
Traditional PostgreSQLINTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'
Abbreviated PostgreSQLINTERVAL '1 yr 2 mons 3 d 4 hrs 5 mins 6 secs'

CockroachDB also supports using uninterpretedstring literals in contextswhere an INTERVAL value is otherwise expected.

Size

An INTERVAL column supports values up to 24 bytes in width, but the total storage size is likely to be larger due to CockroachDB metadata. Intervals are stored internally as months, days, and microseconds.

Precision

New in v19.1: Intervals are stored with microsecond precision instead of nanoseconds, and it is no longer possible to create intervals with nanosecond precision. As a result, parsing from a string or converting from a float or decimal will round to the nearest microsecond, as will any arithmetic operation (add, sub, mul, div) on intervals. CockroachDB rounds (instead of truncating) to match the behavior of Postgres.

Warning:

When upgrading to 19.1, existing intervals with nanoseconds will no longer be able to return their nanosecond part. An existing table t with nanoseconds in intervals of column s can round them to the nearest microsecond with UPDATE t SET s = s + '0s'. Note that this could cause uniqueness problems if the interval is being used as a primary key.

Example

  1. > CREATE TABLE intervals (a INT PRIMARY KEY, b INTERVAL);
  1. > SHOW COLUMNS FROM intervals;
  1. column_name | data_type | is_nullable | column_default | generation_expression | indices | is_hidden
  2. -------------+-----------+-------------+----------------+-----------------------+-----------+-----------
  3. a | INT8 | f | | | {primary} | f
  4. b | INTERVAL | t | | | {} | f
  5. (2 rows)
  1. > INSERT INTO
  2. intervals
  3. VALUES (1, INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'),
  4. (2, INTERVAL '1-2 3 4:5:6'),
  5. (3, '1-2 3 4:5:6');
  1. > SELECT * FROM intervals;
  1. a | b
  2. ---+-------------------------------
  3. 1 | 1 year 2 mons 3 days 04:05:06
  4. 2 | 1 year 2 mons 3 days 04:05:06
  5. 3 | 1 year 2 mons 3 days 04:05:06
  6. (3 rows)

Supported casting and conversion

INTERVAL values can be cast to any of the following data types:

TypeDetails
INTConverts to number of seconds (second precision)
DECIMALConverts to number of seconds (microsecond precision)
FLOATConverts to number of picoseconds
STRINGConverts to h-m-s format (microsecond precision)
TIMEConverts to HH:MM:SS.SSSSSS, the time equivalent to the interval after midnight (microsecond precision)

See also

Data Types

Was this page helpful?
YesNo