SQL reference (MySQL and MariaDB dialects)

Date and time types

DATE, DATETIME, TIMESTAMP, TIME and YEAR with fractional seconds: accepted forms, ranges, rounding and zero dates.

Syntax#

SQL
DATE
DATETIME [(fsp)]
TIMESTAMP [(fsp)]
TIME [(fsp)]
YEAR

fsp is the number of fractional-second digits, 0 to 6 (default 0).

Ranges and forms#

TypeWritten asRange
DATEYYYY-MM-DDfrom 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 samethe 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
YEARYYYY1901 to 2155, and 0

Remarks#

  • Forms read. Dates are read with - as separator and T or a space between date and time; the time part of a DATE value is checked and dropped; a DATETIME given a date alone gets 00:00:00; a TIME may have one digit of hour ('1:2:3' is 01:02:03) and may leave out the seconds. Forms without separators (20240101) and two-digit years are not read.
  • Calendar. 2023-02-29 and month 13 are errors. Years below 1000 are refused, except the zero date.
  • Fractional seconds. A column keeps exactly fsp digits and is always shown with them (DATETIME(3) shows 2024-03-29 11:37:46.123). Extra digits in the value are rounded to fsp digits, half up, in the MySQL dialect and dropped (truncated) in the MariaDB dialect. NOW(n) and DEFAULT CURRENT_TIMESTAMP(n) take the number of digits. fsp above 6 is error 1426. information_schema.COLUMNS.DATETIME_PRECISION shows the digits.
  • Defaults. DEFAULT CURRENT_TIMESTAMP[(n)] (also NOW(), LOCALTIME, LOCALTIMESTAMP) is read for DATETIME and TIMESTAMP; a TIMESTAMP NULL column is shown as timestamp NULL in SHOW CREATE TABLE. ON UPDATE CURRENT_TIMESTAMP in a column definition is not supported yet.
  • Time zone. Raft DB runs in UTC: TIMESTAMP does not convert between the session zone and UTC, and NOW() is UTC.
  • Zero dates. 0000-00-00 and dates with a zero month or day are accepted only if the sql_mode does not have NO_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 for datetime and time; SQLSTATE 22007); an out-of-range YEAR is 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. YEAR as a function returns the year of a date and has the type YEAR.

Examples#

SQL
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 1264

Example.

See also#