13.2.1 Date and Time Data Type Syntax (original) (raw)

13.2.1 Date and Time Data Type Syntax

The date and time data types for representing temporal values are DATE,TIME,DATETIME,TIMESTAMP, andYEAR.

For the DATE andDATETIME range descriptions,“supported” means that although earlier values might work, there is no guarantee.

MySQL permits fractional seconds forTIME,DATETIME, andTIMESTAMP values, with up to microseconds (6 digits) precision. To define a column that includes a fractional seconds part, use the syntax_`typename`_(_`fsp`_), where typename isTIME,DATETIME, orTIMESTAMP, and_fsp_ is the fractional seconds precision. For example:

CREATE TABLE t1 (t TIME(3), dt DATETIME(6), ts TIMESTAMP(0));

The fsp value, if given, must be in the range 0 to 6. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0. (This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.)

Any TIMESTAMP orDATETIME column in a table can have automatic initialization and updating properties; seeSection 13.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

The SUM() andAVG() aggregate functions do not work with temporal values. (They convert the values to numbers, losing everything after the first nonnumeric character.) To work around this problem, convert to numeric units, perform the aggregate operation, and convert back to a temporal value. Examples:

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name;
SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name;