日期和时间值可以用多种格式表示,例如带引号的字符串或数字,具体取决于值的确切类型和其他因素。例如,在 MySQL 需要日期的上下文中,它将 、 和 中的任何一个解释
'2015-07-21'
为'20150721'
日期20150721
。
本节介绍日期和时间文字可接受的格式。有关时间数据类型的更多信息,例如允许值的范围,请参阅 第 11.2 节,“日期和时间数据类型”。
标准 SQL 需要使用类型关键字和字符串指定时间文字。关键字和字符串之间的空格是可选的。
DATE 'str'
TIME 'str'
TIMESTAMP 'str'
MySQL 可以识别,但与标准 SQL 不同,它不需要 type 关键字。符合标准的应用程序应该包括时间文字的类型关键字。
MySQL 还识别与标准 SQL 语法相对应的 ODBC 语法:
{ d 'str' }
{ t 'str' }
{ ts 'str' }
MySQL 使用类型关键字和 ODBC 构造分别生成DATE
、
TIME
和
DATETIME
值,如果指定,包括尾随的小数秒部分。该
TIMESTAMP
语法
DATETIME
在 MySQL 中生成一个值,因为
DATETIME
其范围更接近于标准 SQL
TIMESTAMP
类型,其年份范围从0001
到
9999
. (MySQL
TIMESTAMP
年份范围是
1970
到2038
。)
MySQL 识别DATE
这些格式的值:
作为 或 格式的字符串。允许使用“宽松”语法:任何标点符号都可以用作日期部分之间的分隔符。例如,、 、 和 是等价的。
'
YYYY-MM-DD
''
YY-MM-DD
''2012-12-31'
'2012/12/31'
'2012^12^31'
'2012@12@31'
As a string with no delimiters in either
'
orYYYYMMDD
''
format, provided that the string makes sense as a date. For example,YYMMDD
''20070523'
and'070523'
are interpreted as'2007-05-23'
, but'071332'
is illegal (it has nonsensical month and day parts) and becomes'0000-00-00'
.As a number in either
YYYYMMDD
orYYMMDD
format, provided that the number makes sense as a date. For example,19830905
and830905
are interpreted as'1983-09-05'
.
MySQL recognizes DATETIME
and
TIMESTAMP
values in these
formats:
As a string in either
'
orYYYY-MM-DD hh:mm:ss
''
format. A “relaxed” syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example,YY-MM-DD hh:mm:ss
''2012-12-31 11:30:45'
,'2012^12^31 11+30+45'
,'2012/12/31 11*30*45'
, and'2012@12@31 11^30^45'
are equivalent.The only delimiter recognized between a date and time part and a fractional seconds part is the decimal point.
The date and time parts can be separated by
T
rather than a space. For example,'2012-12-31 11:30:45'
'2012-12-31T11:30:45'
are equivalent.As a string with no delimiters in either
'
orYYYYMMDDhhmmss
''
format, provided that the string makes sense as a date. For example,YYMMDDhhmmss
''20070523091528'
and'070523091528'
are interpreted as'2007-05-23 09:15:28'
, but'071122129015'
is illegal (it has a nonsensical minute part) and becomes'0000-00-00 00:00:00'
.As a number in either
YYYYMMDDhhmmss
orYYMMDDhhmmss
format, provided that the number makes sense as a date. For example,19830905132800
and830905132800
are interpreted as'1983-09-05 13:28:00'
.
A DATETIME
or
TIMESTAMP
value can include a
trailing fractional seconds 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.7, “Fractional Seconds in Time Values”.
Dates containing two-digit year values are ambiguous because the century is unknown. MySQL interprets two-digit year values using these rules:
Year values in the range
70-99
become1970-1999
.Year values in the range
00-69
become2000-2069
.
See also Section 11.2.9, “2-Digit Years in Dates”.
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
.
'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
.
'2015-10-30 1:2:3'
is the same as
'2015-10-30 01:02:03'
.
Values specified as numbers should be 6, 8, 12, or 14 digits
long. 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. 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. 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.
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. 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.
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'
.
MySQL recognizes TIME
values in
these formats:
As a string in
'D hh:mm:ss'
format. You can also use one of the following “relaxed” syntaxes:'hh:mm:ss'
,'hh:mm'
,'D hh:mm'
,'D hh'
, or'ss'
. HereD
represents days and can have a value from 0 to 34.As a string with no delimiters in
'hhmmss'
format, provided that it makes sense as a time. For example,'101112'
is understood as'10:11:12'
, but'109712'
is illegal (it has a nonsensical minute part) and becomes'00:00:00'
.As a number in
hhmmss
format, provided that it makes sense as a time. For example,101112
is understood as'10:11:12'
. The following alternative formats are also understood:ss
,mmss
, orhhmmss
.
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.7, “Fractional Seconds in Time Values”.
对于TIME
指定为包含时间部分定界符的字符串的值,无需为小于 的小时、分钟或秒值指定两位数10
。
'8:3:2'
与 相同
'08:03:02'
。