Date and time types
DATE, DATETIME, TIMESTAMP, TIME and YEAR with fractional seconds: accepted forms, ranges, rounding and zero dates.
Syntax#
DATE
DATETIME [(fsp)]
TIMESTAMP [(fsp)]
TIME [(fsp)]
YEARfsp is the number of fractional-second digits, 0 to 6 (default 0).
Ranges and forms#
| Type | Written as | Range |
|---|---|---|
DATE | YYYY-MM-DD | from 1000-01-01 (and the zero date) |
DATETIME[(fsp)] | YYYY-MM-DD HH:MM:SS[.ffffff] | the date range, with a time of day |
TIMESTAMP[(fsp)] | the same | the same as DATETIME: Raft DB does not limit it to 1970 to 2038 |
TIME[(fsp)] | [-]HH:MM:SS[.ffffff] | -838:59:59 to 838:59:59 |
YEAR | YYYY | 1901 to 2155, and 0 |
Remarks#
- Forms read. Dates are read with
-as separator andTor a space between date and time; the time part of aDATEvalue is checked and dropped; aDATETIMEgiven a date alone gets00:00:00; aTIMEmay have one digit of hour ('1:2:3'is01:02:03) and may leave out the seconds. Forms without separators (20240101) and two-digit years are not read. - Calendar.
2023-02-29and month 13 are errors. Years below 1000 are refused, except the zero date. - Fractional seconds. A column keeps exactly
fspdigits and is always shown with them (DATETIME(3)shows2024-03-29 11:37:46.123). Extra digits in the value are rounded tofspdigits, half up, in the MySQL dialect and dropped (truncated) in the MariaDB dialect.NOW(n)andDEFAULT CURRENT_TIMESTAMP(n)take the number of digits.fspabove 6 is error 1426.information_schema.COLUMNS.DATETIME_PRECISIONshows the digits. - Defaults.
DEFAULT CURRENT_TIMESTAMP[(n)](alsoNOW(),LOCALTIME,LOCALTIMESTAMP) is read forDATETIMEandTIMESTAMP; aTIMESTAMP NULLcolumn is shown astimestamp NULLinSHOW CREATE TABLE.ON UPDATE CURRENT_TIMESTAMPin a column definition is not supported yet. - Time zone. Raft DB runs in UTC:
TIMESTAMPdoes not convert between the session zone and UTC, andNOW()is UTC. - Zero dates.
0000-00-00and dates with a zero month or day are accepted only if thesql_modedoes not haveNO_ZERO_DATE/NO_ZERO_IN_DATE(the MySQL default has both, the MariaDB default has neither listed, so they are allowed there). See SQL modes. - Invalid values. In strict mode error 1292 (
The date value '2023-02-29' given for column 'dt' (row 1) is not valid, and the same fordatetimeandtime; SQLSTATE 22007); an out-of-rangeYEARis error 1264. Without strict mode the zero value of the type is stored with warning 1265. - Arithmetic and functions. The date functions work with whole seconds (see Date and time functions); comparison of two datetime texts is by text, which is correct for this form.
YEARas a function returns the year of a date and has the typeYEAR.
Examples#
CREATE TABLE t (id INT PRIMARY KEY, d0 DATETIME, d3 DATETIME(3), d6 DATETIME(6), ts TIMESTAMP(6) NULL, tm TIME(3), tm0 TIME);
INSERT INTO t VALUES (1, '2024-03-29 11:37:46', '2024-03-29 11:37:46.123456', '2024-03-29 11:37:46.123456', '2024-03-29 11:37:46.5', '10:20:30.9876', '10:20:30.5');
INSERT INTO t VALUES (2, '2024-03-29 11:37:46.7', '2024-03-29 11:37:46', '2024-03-29', NULL, '1:2:3', '838:59:59');
SELECT d6 FROM t WHERE d6 = '2024-03-29 11:37:46.123456';
CREATE TABLE t2 (id INT PRIMARY KEY, c DATETIME(6) DEFAULT CURRENT_TIMESTAMP(6), c0 TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
CREATE TABLE t3 (x DATETIME(7)); -- error 1426
INSERT INTO types (dt) VALUES ('2023-02-29'); -- error 1292
INSERT INTO types (y) VALUES (1800); -- error 1264Example.