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
> CREATE TABLE dates (a DATE PRIMARY KEY, b INT);
> SHOW COLUMNS FROM dates;
+-------------+-----------+-------------+----------------+-----------------------+-------------+
| column_name | data_type | is_nullable | column_default | generation_expression | indices |
+-------------+-----------+-------------+----------------+-----------------------+-------------+
| a | DATE | false | NULL | | {"primary"} |
| b | INT | true | NULL | | {} |
+-------------+-----------+-------------+----------------+-----------------------+-------------+
(2 rows)
Explicitly typed DATE
literal:
> INSERT INTO dates VALUES (DATE '2016-03-26', 12345);
String literal implicitly typed as DATE
:
> INSERT INTO dates VALUES ('2016-03-27', 12345);
> SELECT * FROM dates;
+---------------------------+-------+
| a | b |
+---------------------------+-------+
| 2016-03-26 00:00:00+00:00 | 12345 |
| 2016-03-27 00:00:00+00:00 | 12345 |
+---------------------------+-------+
Supported casting and conversion
DATE
values can be cast to any of the following data types:
Type | Details |
---|---|
DECIMAL | Converts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice. |
FLOAT | Converts to number of days since the Unix epoch (Jan. 1, 1970). This is a CockroachDB experimental feature which may be changed without notice. |
TIMESTAMP | Sets the time to 00:00 (midnight) in the resulting timestamp |
INT | Converts 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
当前内容版权归 cockroachlabs.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 cockroachlabs.com .