9.1.3 Date and Time Literals日期和时间文字

Date and time values can be represented in several formats, such as quoted strings or as numbers, depending on the exact type of the value and other factors. 日期和时间值可以用几种格式表示,例如带引号的字符串或数字,具体取决于值的确切类型和其他因素。For example, in contexts where MySQL expects a date, it interprets any of '2015-07-21', '20150721', and 20150721 as a date.例如,在MySQL需要日期的上下文中,它会将'2015-07-21''20150721'20150721中的任何一个解释为日期。

This section describes the acceptable formats for date and time literals. 本节介绍日期和时间文字的可接受格式。For more information about the temporal data types, such as the range of permitted values, see Section 11.2, “Date and Time Data Types”.有关时间数据类型的更多信息,如允许值的范围,请参阅第11.2节,“日期和时间数据类型”

Standard SQL and ODBC Date and Time Literals.标准SQL和ODBC日期和时间文本。  Standard SQL requires temporal literals to be specified using a type keyword and a string. 标准SQL要求使用类型关键字和字符串指定时态文字。The space between the keyword and string is optional.关键字和字符串之间的空格是可选的。

DATE 'str'
TIME 'str'
TIMESTAMP 'str'

MySQL recognizes but, unlike standard SQL, does not require the type keyword. MySQL可以识别,但与标准SQL不同,它不需要type关键字。Applications that are to be standard-compliant should include the type keyword for temporal literals.符合标准的应用程序应该包括时态文字的type关键字。

MySQL also recognizes the ODBC syntax corresponding to the standard SQL syntax:MySQL还可以识别与标准SQL语法对应的ODBC语法:

{ d 'str' }
{ t 'str' }
{ ts 'str' }

MySQL uses the type keywords and the ODBC constructions to produce DATE, TIME, and DATETIME values, respectively, including a trailing fractional seconds part if specified. The TIMESTAMP syntax produces a DATETIME value in MySQL because DATETIME has a range that more closely corresponds to the standard SQL TIMESTAMP type, which has a year range from 0001 to 9999. (The MySQL TIMESTAMP year range is 1970 to 2038.)

String and Numeric Literals in Date and Time Context.  MySQL recognizes DATE values in these formats:

MySQL recognizes DATETIME and TIMESTAMP values in these formats:

A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. DATETIMETIMESTAMP值可以包含精度高达微秒(6位)的尾随小数秒部分。The fractional part should always be separated from the rest of the time by a decimal point; no other fractional seconds delimiter is recognized. 小数部分应始终与其余时间间隔一个小数点;无法识别其他小数秒分隔符。For information about fractional seconds support in MySQL, see Section 11.2.6, “Fractional Seconds in Time Values”.有关MySQL中分数秒支持的信息,请参阅第11.2.6节,“时间值中的分数秒”

Dates containing two-digit year values are ambiguous because the century is unknown. 包含两位数年份值的日期不明确,因为世纪未知。MySQL interprets two-digit year values using these rules:MySQL使用以下规则解释两位数的年份值:

See also Section 11.2.8, “2-Digit Years in Dates”.另见第11.2.8节,“日期中的两位数年份”

For values specified as strings that include date part delimiters, it is unnecessary to specify two digits for month or day values that are less than 10. 对于指定为包含日期部分分隔符的字符串的值,没有必要为小于10的月或日值指定两位数字。'2015-6-9' is the same as '2015-06-09'. Similarly, for values specified as strings that include time part delimiters, it is unnecessary to specify two digits for hour, minute, or second values that are less than 10. 类似地,对于指定为包含时间段分隔符的字符串的值,不需要为小于10的小时、分钟或秒值指定两位数字。'2015-10-30 1:2:3' is the same as '2015-10-30 01:02:03'.'2015-10-30 1:2:3''2015-10-30 01:02:03'相同。

Values specified as numbers should be 6, 8, 12, or 14 digits long. 指定为数字的值应为6、8、12或14位。If a number is 8 or 14 digits long, it is assumed to be in YYYYMMDD or YYYYMMDDhhmmss format and that the year is given by the first 4 digits. 如果一个数字的长度为8或14位,则假定它是YYYYMMDDYYYYMMDDhhmmss格式,并且年份由前4位数字给出。If the number is 6 or 12 digits long, it is assumed to be in YYMMDD or YYMMDDhhmmss format and that the year is given by the first 2 digits. 如果数字长度为6或12位,则假定为YYMMDDYYMMDDhhmmss格式,年份由前两位数字给出。Numbers that are not one of these lengths are interpreted as though padded with leading zeros to the closest length.不属于这些长度之一的数字被解释为用前导零填充到最接近的长度。

Values specified as nondelimited strings are interpreted according their length. 指定为非限定字符串的值将根据其长度进行解释。For a string 8 or 14 characters long, the year is assumed to be given by the first 4 characters. 对于长度为8或14个字符的字符串,假设年份由前4个字符给出。Otherwise, the year is assumed to be given by the first 2 characters. 否则,假设年份由前两个字符给出。The string is interpreted from left to right to find year, month, day, hour, minute, and second values, for as many parts as are present in the string. 该字符串从左到右进行解释,以查找年、月、日、小时、分钟和秒的值,其部分与字符串中存在的部分相同。This means you should not use strings that have fewer than 6 characters. 这意味着您不应该使用少于6个字符的字符串。For example, if you specify '9903', thinking that represents March, 1999, MySQL converts it to the zero date value. This occurs because the year and month values are 99 and 03, but the day part is completely missing. 出现这种情况是因为年和月的值分别为9903,但完全缺少日部分。However, you can explicitly specify a value of zero to represent missing month or day parts. 但是,您可以显式指定一个零值来表示缺少的月份或日期部分。For example, to insert the value '1999-03-00', use '990300'.例如,要插入值'1999-03-00',请使用'990300'

MySQL recognizes TIME values in these formats:MySQL以以下格式识别TIME值:

A trailing fractional seconds part is recognized in the 'D hh:mm:ss.fraction', 'hh:mm:ss.fraction', 'hhmmss.fraction', and hhmmss.fraction time formats, where fraction is the fractional part in up to microseconds (6 digits) precision. The fractional part should always be separated from the rest of the time by a decimal point; no other fractional seconds delimiter is recognized. For information about fractional seconds support in MySQL, see Section 11.2.6, “Fractional Seconds in Time Values”.

For TIME values specified as strings that include a time part delimiter, it is unnecessary to specify two digits for hours, minutes, or seconds values that are less than 10. '8:3:2' is the same as '08:03:02'.