DATE

The DATE data type stores a year, month, and day.

Syntax

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

The string format for dates is YYYY-MM-DD. For example: DATE '2016-12-23'.

CockroachDB also supports using uninterpretedstring literals in contextswhere a DATE value is otherwise expected.

Size

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

Examples

  1. > CREATE TABLE dates (a DATE PRIMARY KEY, b INT);
  1. > SHOW COLUMNS FROM dates;
  1. +-------------+-----------+-------------+----------------+-----------------------+-------------+
  2. | column_name | data_type | is_nullable | column_default | generation_expression | indices |
  3. +-------------+-----------+-------------+----------------+-----------------------+-------------+
  4. | a | DATE | false | NULL | | {"primary"} |
  5. | b | INT | true | NULL | | {} |
  6. +-------------+-----------+-------------+----------------+-----------------------+-------------+
  7. (2 rows)

Explicitly typed DATE literal:

  1. > INSERT INTO dates VALUES (DATE '2016-03-26', 12345);

String literal implicitly typed as DATE:

  1. > INSERT INTO dates VALUES ('2016-03-27', 12345);
  1. > SELECT * FROM dates;
  1. +---------------------------+-------+
  2. | a | b |
  3. +---------------------------+-------+
  4. | 2016-03-26 00:00:00+00:00 | 12345 |
  5. | 2016-03-27 00:00:00+00:00 | 12345 |
  6. +---------------------------+-------+

Supported casting and conversion

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

TypeDetails
DECIMALConverts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice.
FLOATConverts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice.
TIMESTAMPSets the time to 00:00 (midnight) in the resulting timestamp
INTConverts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice.
STRING––

See also

Data Types

Was this page helpful?
YesNo