Time and Date
On this page
Data Type |
Size Size (Not Null) |
Resolution |
Min Value |
Max Value |
Zero Value |
Precision |
---|---|---|---|---|---|---|
DATE |
8 bytes 4 bytes |
day |
1000-01-01 |
9999-12-31 |
0000-00-00 |
yyyy-mm-dd |
TIME |
8 bytes 4 bytes |
second |
-838:59:59 |
838:59:59 |
00:00:00 |
hhh:mm:ss |
TIME(6) |
8 bytes 4 bytes |
microsecond |
-838:59:59. |
838:59:59. |
00:00:00. |
hhh:mm:ss. |
DATETIME |
12 bytes 8 bytes |
second |
1000-01-01 00:00:00 |
9999-12-31 23:59:59 |
0000-00-00 00:00:00 |
yyyy-mm-dd hh:mm:ss |
DATETIME(6) |
12 bytes 8 bytes |
microsecond |
1000-01-01 00:00:00. |
9999-12-31 23:59:59. |
0000-00-00 00:00:00. |
yyyy-mm-dd hh:mm:ss. |
TIMESTAMP |
4 bytes 4 bytes |
second |
1970-01-01 00:00:01 |
2038-01-19 03:14:07 |
0000-00-00 00:00:00 |
yyyy-mm-dd hh:mm:ss |
TIMESTAMP(6) |
8 bytes 8 bytes |
microsecond |
1970-01-01 00:00:01. |
2038-01-19 03:14:07. |
0000-00-00 00:00:00. |
yyyy-mm-dd hh:mm:ss. |
YEAR |
2 bytes 1 byte |
year |
1901 |
2155 |
0000 |
yyyy |
Any data being ingested that does not match the precision of the destination data type will be converted.2020-12-31 23:59:59
being inserted into a column with the data type of DATE
would be converted to 2020-12-31
.
Note: SingleStore recommends using DATETIME
or DATETIME(6)
column types instead of TIMESTAMP
or TIMESTAMP(6)
because TIMESTAMP
types only support dates through 2038-01-19
as opposed to 9999-12-31
for DATETIME
types.
Storing Times and Dates
You can use a string literal to store a time value or a date value.t
contains a column dt
of type DATETIME
, you can use the following statement to store the literal.
INSERT INTO t(dt) VALUES ('2020-11-21');
Alternatively, you can use the TO_
Date and Time Functions, such as TO_
and TO_
to explicitly convert a string literal to a time value or a date value.
Inserting or Updating a Column With the Current Timestamp
To allow inserts or updates of a TIMESTAMP
, TIMESTAMP(6)
, DATETIME
, or a DATETIME(6)
column with the current timestamp, define the column with the clause:
-
DEFAULT <timefunction>
, to insert a column with the current timestamp. -
ON UPDATE <timefunction>
, to update a column with the current timestamp. -
DEFAULT <timefunction> ON UPDATE <timefunction>
, to insert and update a column with the current timestamp.
where <timefunction>
is one of the following values: NOW()
, NOW(6)
, CURRENT_
, CURRENT_
.
Examples
The following example demonstrates the use of DEFAULT CURRENT_
in column definition:
CREATE TABLE s(id INT, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP());INSERT INTO s(id) VALUES(1);
SELECT * FROM s;
+------+---------------------+
| id | ts |
+------+---------------------+
| 1 | 2020-05-21 09:12:25 |
+------+---------------------+
The following example demonstrates the use of ON UPDATE CURRENT_
in column definition:
CREATE TABLE s1(id INT, ts DATETIME(6) ON UPDATE CURRENT_TIMESTAMP(6));INSERT INTO s1(id,ts) VALUES(1,"2017-07-01");
SELECT * FROM s1;
+------+----------------------------+
| id | ts |
+------+----------------------------+
| 1 | 2017-07-01 00:00:00.000000 |
+------+----------------------------+
UPDATE s1 SET id = 2 WHERE id = 1;
SELECT * FROM s1;
+------+----------------------------+
| id | ts |
+------+----------------------------+
| 2 | 2020-05-21 09:36:47.124979 |
+------+----------------------------+
The following example demonstrates the use of DEFAULT NOW(6) ON UPDATE NOW(6)
in column definition:
CREATE TABLE s2(id INT, ts DATETIME(6) DEFAULT NOW(6) ON UPDATE NOW(6));INSERT INTO s2(id) VALUES(1);INSERT INTO s2(id) VALUES(2);
SELECT * FROM s2;
+------+----------------------------+
| id | ts |
+------+----------------------------+
| 2 | 2020-05-21 11:21:30.998275 |
| 1 | 2020-05-21 11:21:30.992868 |
+------+----------------------------+
UPDATE s2 SET id = 2 WHERE id = 1;
SELECT * FROM s2;
+------+----------------------------+
| id | ts |
+------+----------------------------+
| 2 | 2020-05-21 11:21:30.998275 |
| 2 | 2020-05-21 11:23:39.134793 |
+------+----------------------------+
Warning
Prior to SingleStore 7, when you inserted or updated a record, the first TIMESTAMP
or TIMESTAMP(6)
column would automatically be populated with the current timestamp.DEFAULT CURRENT_
or ON UPDATE CURRENT_
clause in their column definitions.TIMESTAMP
or TIMESTAMP(6)
columns without these clauses will be populated with a NULL
value instead of the current timestamp.NULL
values are populated in the ts
column in the following example, when run on SingleStore 7 or later.
CREATE TABLE r(d DATETIME(6), ts TIMESTAMP(6), id INT);INSERT r(d, id) VALUES("2017-07-01", 1);
SELECT * FROM r;
+----------------------------+----------------------------+------+
| d | ts | id |
+----------------------------+----------------------------+------+
| 2017-07-01 00:00:00.000000 | NULL | 1 |
+----------------------------+----------------------------+------+
UPDATE r SET id = 2 WHERE id = 1;
SELECT * FROM r;
+----------------------------+----------------------------+------+
| d | ts | id |
+----------------------------+----------------------------+------+
| 2017-07-01 00:00:00.000000 | NULL | 2 |
+----------------------------+----------------------------+------+
The explicit_ defaults_ for_ timestamp Variable
The variable explicit_
determines if certain nonstandard behaviors for default values and NULL-value are disabled for the TIMESTAMP
column.
If the explicit_
variable is enabled, SingleStore handles the TIMESTAMP
column behavior as follows:
-
Assigning a
TIMESTAMP
column value ofNULL
does not set it to the current timestamp.To set to the current timestamp, set the TIMESTAMP
column toCURRENT_
orTIMESTAMP() NOW()
. -
If the
TIMESTAMP
column is not declared asNOT NULL
, then it is automatically declared with theNULL
attribute and permitsNULL
values.Assigning a value of NULL
to the column sets it toNULL
, not the current timestamp. -
TIMESTAMP
column declared with theNOT NULL
attribute do not permitNULL
values. -
TIMESTAMP
columns explicitly declared with theNOT NULL
attribute and without an explicitDEFAULT
attribute are treated as having no default value. -
To declare
TIMESTAMP
column with theDEFAULT CURRENT_
orTIMESTAMP() ON UPDATE CURRENT_
attributes, these must be explicitly specified.TIMESTAMP() -
The first
TIMESTAMP
column in a table is handled in the same way as theTIMESTAMP
columns following the first one.
If the explicit_
variable is disabled, SingleStore handles the TIMESTAMP
column behavior as follows:
-
If the
TIMESTAMP
column is not declared asNULL
, then it is automatically declared with theNOT NULL
attribute.Assigning a value of NULL
to the column is permitted and sets the column to the current timestamp. -
The first
TIMESTAMP
column in a table, if not declared asNULL
,DEFAULT CURRENT_
orTIMESTAMP() ON UPDATE CURRENT_
, is automatically declared with theTIMESTAMP() DEFAULT CURRENT_
andTIMESTAMP() ON UPDATE CURRENT_
attributes.TIMESTAMP() -
TIMESTAMP
columns following the first one, if not declared with theNULL
attribute or aDEFAULT
attribute, are automatically declared asDEFAULT
0000-00-00 00:00:00
(thezero
timestamp).
Handling of Time Zones
SingleStore currently ignores any time zone offsets on query writes and reads for DATETIME
and TIMESTAMP
data types.
See the following examples for DATETIME
and TIMESTAMP
respectively:
Note
SingleStore recommends that you keep your system_
at UTC and then have your application convert from UTC to the user’s local time zone.
CREATE TABLE dt (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,col DATETIME NOT NULL);INSERT INTO dt (col) VALUES ('2022-01-01 10:10:10'),('2022-01-01 10:10:10+05:30'),('2022-01-01 10:10:10-08:00');
SELECT * FROM dt;
+----+---------------------+
| id | col |
+----+---------------------+
| 1 | 2022-01-01 10:10:10 |
| 2 | 2022-01-01 10:10:10 |
| 3 | 2022-01-01 10:10:10 |
+----+---------------------+
CREATE TABLE ts (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,col TIMESTAMP NOT NULL);INSERT INTO ts (col) VALUES ('2022-01-01 10:10:10'),('2022-01-01 10:10:10+05:30'),('2022-01-01 10:10:10-08:00');
SELECT * FROM ts;
+----+---------------------+
| id | col |
+----+---------------------+
| 1 | 2022-01-01 10:10:10 |
| 2 | 2022-01-01 10:10:10 |
| 3 | 2022-01-01 10:10:10 |
+----+---------------------+
Note
Time zone offsets are also ignored when data is ingested via LOAD DATA
and or pipelines.
If the data_
is 7.
or higher and if any extra characters are included in the query (including a timezone), an error will be generated.
SELECT @@data_conversion_compatibility_level;
+---------------------------------------+
| @@data_conversion_compatibility_level |
+---------------------------------------+
| 7.5 |
+---------------------------------------+
CREATE TABLE dt1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, col DATETIME NOT NULL);INSERT INTO dt1 (col) VALUES ('2022-01-01 10:10:10'),('2022-01-01 10:10:10+05:30'),('2022-01-01 10:10:10-08:00');
ERROR 2363 (HY000): Leaf Error (127.0.0.1:3307): Invalid DATE/TIME in type conversion
Zero Value Time and Date
The ability to enter a zero value into a time or date column (for example, 0000 for a YEAR column) is controlled by the data_
.
CREATE TABLE ztest(id int, zy year);
INSERT INTO ztest VALUES(2, 0000);
ERROR 1264 (22003): Leaf Error (127.0.0.1:3306): Out of range value for column 'zy'
SELECT @@data_conversion_compatibility_level;
+---------------------------------------+
| @@data_conversion_compatibility_level |
+---------------------------------------+
| 8.0 |
+---------------------------------------+
Entering a zero value for year is restricted via data_
.
SET GLOBAL data_conversion_compatibility_level = '6.0';INSERT INTO ztest VALUES(2, 0000);SELECT * FROM ztest;
+------+------+
| id | zy |
+------+------+
| 2 | 0000 |
+------+------+
1 row in set (0.321 sec)
Last modified: September 20, 2024