DateTime Types
AttentionThis page documents an earlier version. Go to the latest (v2.1)version.
Synopsis
Datetime datatypes are used to specify data of date and time at a timezone, DATE
for a specific day, TIME
for time of day, and TIMESTAMP
for the combination of both date and time.Of the three, Yugabyte currently only supports the TIMESTAMP
type.
Syntax
type_specification ::= TIMESTAMP | DATE | TIME
timestamp_format ::= date_format [ time_format ] [ timezone_format ]
date_format ::= digit digit digit digit '-' digit [ digit ] '-' digit [ digit ]
time_format ::= digit [ digit ] [ ':' digit [ digit ] [ ':' digit [digit] [ '.' digit [ digit [ digit ] ] ] ] ]
timezone_format ::= [ 'UTC' ] ( '+' | '-' ) digit [ digit ] ':' digit [ digit ]
Where
- the
timestamp_format
given above is not the timestamp literal but is used to match text literals when converting them toTIMESTAMP
type.
Semantics
- Columns of type
TIMESTAMP
can be part of thePRIMARY KEY
. - Implicitly, value of type datetime type are neither convertible nor comparable to other datatypes.
- Values of integer and text datatypes with the correct format (given above) are convertible to datetime types.
- Supported timestamp range is from year
1900
to year9999
. - If not specified, the default value for hour, minute, second, and millisecond components is
0
. - If not specified, the default timezone is UTC.
Examples
Using the timestamp type
You can do this as shown below.
cqlsh:example> CREATE TABLE sensor_data(sensor_id INT, ts TIMESTAMP, value FLOAT, PRIMARY KEY(sensor_id, ts));
cqlsh:example> -- Timestamp values can be given using date-time literals
cqlsh:example> INSERT INTO sensor_data(sensor_id, ts, value) VALUES (1, '2017-07-04 12:30:30 UTC', 12.5);
cqlsh:example> INSERT INTO sensor_data(sensor_id, ts, value) VALUES (1, '2017-07-04 12:31 UTC', 13.5);
Timestamp values can also be given as integers (milliseconds from epoch).
cqlsh:example> INSERT INTO sensor_data(sensor_id, ts, value) VALUES (2, 1499171430000, 20);
cqlsh:example> SELECT * FROM sensor_data;
sensor_id | ts | value
-----------+---------------------------------+-------
2 | 2017-07-04 12:30:30.000000+0000 | 20
1 | 2017-07-04 12:30:30.000000+0000 | 12.5
1 | 2017-07-04 12:31:00.000000+0000 | 13.5
Supported timestamp literals
'1992-06-04 12:30'
'1992-6-4 12:30'
'1992-06-04 12:30+04:00'
'1992-6-4 12:30-04:30'
'1992-06-04 12:30 UTC+04:00'
'1992-6-4 12:30 UTC-04:30'
'1992-06-04 12:30.321'
'1992-6-4 12:30.12'
'1992-06-04 12:30.321+04:00'
'1992-6-4 12:30.12-04:30'
'1992-06-04 12:30.321 UTC+04:00'
'1992-6-4 12:30.12 UTC-04:30'
'1992-06-04 12:30:45'
'1992-6-4 12:30:45'
'1992-06-04 12:30:45+04:00'
'1992-6-4 12:30:45-04:30'
'1992-06-04 12:30:45 UTC+04:00'
'1992-6-4 12:30:45 UTC-04:30'
'1992-06-04 12:30:45.321'
'1992-6-4 12:30:45.12'
'1992-06-04 12:30:45.321+04:00'
'1992-6-4 12:30:45.12-04:30'
'1992-06-04 12:30:45.321 UTC+04:00'
'1992-6-4 12:30:45.12 UTC-04:30'
'1992-06-04T12:30'
'1992-6-4T12:30'
'1992-06-04T12:30+04:00'
'1992-6-4T12:30-04:30'
'1992-06-04T12:30 UTC+04:00'
'1992-6-4T12:30TUTC-04:30'
'1992-06-04T12:30.321'
'1992-6-4T12:30.12'
'1992-06-04T12:30.321+04:00'
'1992-6-4T12:30.12-04:30'
'1992-06-04T12:30.321 UTC+04:00'
'1992-6-4T12:30.12 UTC-04:30'
'1992-06-04T12:30:45'
'1992-6-4T12:30:45'
'1992-06-04T12:30:45+04:00'
'1992-6-4T12:30:45-04:30'
'1992-06-04T12:30:45 UTC+04:00'
'1992-6-4T12:30:45 UTC-04:30'
'1992-06-04T12:30:45.321'
'1992-6-4T12:30:45.12'
'1992-06-04T12:30:45.321+04:00'
'1992-6-4T12:30:45.12-04:30'
'1992-06-04T12:30:45.321 UTC+04:00'
'1992-6-4T12:30:45.12 UTC-04:30'
'1992-06-04'
'1992-6-4'
'1992-06-04+04:00'
'1992-6-4-04:30'
'1992-06-04 UTC+04:00'
'1992-6-4 UTC-04:30'
See Also
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .