TIME

The TIME data type stores the time of day in UTC.

Aliases

In CockroachDB, the following are aliases:

TIME WITHOUT TIME ZONE

Syntax

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

The string format for time is HH:MM:SS.SSSSSS. For example: TIME '05:40:00.000001'.

When it is unambiguous, a simple unannotated string literal can alsobe automatically interpreted as type TIME.

Note that the fractional portion of TIME is optional and is rounded to microseconds (i.e., six digits after the decimal) for compatibility with the PostgreSQL wire protocol.

Size

A TIME column supports values up to 8 bytes in width, but the total storage size is likely to be larger due to CockroachDB metadata.

Example

  1. > CREATE TABLE time (time_id INT PRIMARY KEY, time_val TIME);
  1. > SHOW COLUMNS FROM time;
  1. +-------------+-----------+-------------+----------------+-----------------------+-------------+
  2. | column_name | data_type | is_nullable | column_default | generation_expression | indices |
  3. +-------------+-----------+-------------+----------------+-----------------------+-------------+
  4. | time_id | INT | false | NULL | | {"primary"} |
  5. | time_val | TIME | true | NULL | | {} |
  6. +-------------+-----------+-------------+----------------+-----------------------+-------------+
  7. (2 rows)
  1. > INSERT INTO time VALUES (1, TIME '05:40:00'), (2, TIME '05:41:39');
  1. > SELECT * FROM time;
  1. +---------+---------------------------+
  2. | time_id | time_val |
  3. +---------+---------------------------+
  4. | 1 | 0000-01-01 05:40:00+00:00 |
  5. | 2 | 0000-01-01 05:41:39+00:00 |
  6. +---------+---------------------------+
  7. (2 rows)

Note:
The cockroach sql shell displays the date and time zone due to the Go SQL driver it uses. Other client drivers may behave similarly. In such cases, however, the date and time zone are not relevant and are not stored in the database.

Comparing TIME values:

  1. > SELECT (SELECT time_val FROM time WHERE time_id = 1) < (SELECT time_val FROM time WHERE time_id = 2);
  1. +--------------------------------+
  2. | (SELECT time_val FROM "time" |
  3. | WHERE time_id = 1) < (SELECT |
  4. | time_val FROM "time" WHERE |
  5. | time_id = 2) |
  6. +--------------------------------+
  7. | true |
  8. +--------------------------------+
  9. (1 row)

Supported casting & conversion

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

TypeDetails
INTERVALConverts to the span of time since midnight (00:00)
STRINGConverts to format 'HH:MM:SS.SSSSSS' (microsecond precision)

See also

Was this page helpful?
YesNo