To some extent, you can convert a value from one temporal type to another. 在某种程度上,可以将值从一种时态类型转换为另一种时态类型。However, there may be some alteration of the value or loss of information. 但是,信息的价值可能会发生一些变化或丢失。In all cases, conversion between temporal types is subject to the range of valid values for the resulting type. 在所有情况下,时态类型之间的转换都取决于结果类型的有效值范围。For example, although 例如,尽管DATE
, DATETIME
, and TIMESTAMP
values all can be specified using the same set of formats, the types do not all have the same range of values. DATE
、DATETIME
和TIMESTAMP
值都可以使用相同的格式集指定,但这些类型的值的范围并不都相同。时间戳值不能早于TIMESTAMP
values cannot be earlier than 1970
UTC or later than '2038-01-19 03:14:07'
UTC. 1970
UTC或晚于'2038-01-19 03:14:07'
UTC。This means that a date such as 这意味着诸如'1968-01-01'
, while valid as a DATE
or DATETIME
value, is not valid as a TIMESTAMP
value and is converted to 0
.'1968-01-01'
之类的日期虽然作为DATE
或DATETIME
值有效,但作为TIMESTAMP
值无效,并被转换为0
。
Conversion of DATE
values:DATE
值的转换:
Conversion of DATETIME
and TIMESTAMP
values:DATETIME
和TIMESTAMP
值的转换:
Conversion to a 转换为DATE
value takes fractional seconds into account and rounds the time part. DATE
值需要考虑分数秒并舍入时间部分。For example, 例如,'1999-12-31 23:59:59.499'
becomes '1999-12-31'
, whereas '1999-12-31 23:59:59.500'
becomes '2000-01-01'
.'1999-12-31 23:59:59.499'
变为'1999-12-31'
,而'1999-12-31 23:59:59.500'
变为'2000-01-01'
。
Conversion to a 转换为TIME
value discards the date part because the TIME
type contains no date information.TIME
值将丢弃日期部分,因为TIME
类型不包含日期信息。
For conversion of 为了将TIME
values to other temporal types, the value of CURRENT_DATE()
is used for the date part. TIME
值转换为其他时间类型,日期部分使用CURRENT_DATE()
的值。The TIME
is interpreted as elapsed time (not time of day) and added to the date. TIME
被解释为经过的时间(不是一天中的时间)并添加到日期中。This means that the date part of the result differs from the current date if the time value is outside the range from 这意味着,如果时间值超出'00:00:00'
to '23:59:59'
.'00:00:00'
到'23:59:59'
的范围,则结果的日期部分与当前日期不同。
Suppose that the current date is 假设当前日期为'2012-01-01'
. '2012-01-01'
。TIME
values of '12:00:00'
, '24:00:00'
, and '-12:00:00'
, when converted to DATETIME
or TIMESTAMP
values, result in '2012-01-01 12:00:00'
, '2012-01-02 00:00:00'
, and '2011-12-31 12:00:00'
, respectively.'12:00:00'
、'24:00:00'
和'-12:00:00'
的时间值转换为日期时间或时间戳值时,将分别生成'2012-01-01 12:00:00'
、'2012-01-02 00:00:00'
和'2011-12-31 12:00:00'
。
Conversion of TIME
to DATE
is similar but discards the time part from the result: '2012-01-01'
, '2012-01-02'
, and '2011-12-31'
, respectively.TIME
到DATE
的转换类似,但会从结果中丢弃时间部分:'2012-01-01'
、'2012-01-02'
和'2011-12-31'
。
Explicit conversion can be used to override implicit conversion. 显式转换可用于重写隐式转换。For example, in comparison of 例如,在比较DATE
and DATETIME
values, the DATE
value is coerced to the DATETIME
type by adding a time part of '00:00:00'
. DATE
和DATETIME
值时,通过添加'00:00:00'
的时间部分,日期值被强制为DATETIME
类型。To perform the comparison by ignoring the time part of the 要通过忽略DATETIME
value instead, use the CAST()
function in the following way:DATETIME
值的时间部分来执行比较,请按以下方式使用CAST()
函数:
date_col
= CAST(datetime_col
AS DATE)
Conversion of 将TIME
and DATETIME
values to numeric form (for example, by adding +0
) depends on whether the value contains a fractional seconds part. TIME
和DATETIME
值转换为数字形式(例如,通过添加+0
)取决于该值是否包含小数秒部分。当TIME(
or N
)DATETIME(
is converted to integer when N
)N
is 0 (or omitted) and to a DECIMAL
value with N
decimal digits when N
is greater than 0:N
为0(或省略)时,TIME(
或N
)DATETIME(
被转换为整数;当N
)N
大于0时,TIME(
或N
)DATETIME(
被转换为带N
)N
个十进制数字的DECIMAL
值:
mysql>SELECT CURTIME(), CURTIME()+0, CURTIME(3)+0;
+-----------+-------------+--------------+ | CURTIME() | CURTIME()+0 | CURTIME(3)+0 | +-----------+-------------+--------------+ | 09:28:00 | 92800 | 92800.887 | +-----------+-------------+--------------+ mysql>SELECT NOW(), NOW()+0, NOW(3)+0;
+---------------------+----------------+--------------------+ | NOW() | NOW()+0 | NOW(3)+0 | +---------------------+----------------+--------------------+ | 2012-08-15 09:28:00 | 20120815092800 | 20120815092800.889 | +---------------------+----------------+--------------------+