11.2.7 Conversion Between Date and Time Types日期和时间类型之间的转换

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. 例如,尽管DATEDATETIMETIMESTAMP值都可以使用相同的格式集指定,但这些类型的值的范围并不都相同。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'之类的日期虽然作为DATEDATETIME值有效,但作为TIMESTAMP值无效,并被转换为0

Conversion of DATE values:DATE值的转换:

Conversion of DATETIME and TIMESTAMP values:DATETIMETIMESTAMP值的转换:

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.TIMEDATE的转换类似,但会从结果中丢弃时间部分:'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'. 例如,在比较DATEDATETIME值时,通过添加'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. TIMEDATETIME值转换为数字形式(例如,通过添加+0)取决于该值是否包含小数秒部分。TIME(N) or DATETIME(N) is converted to integer when 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 |
+---------------------+----------------+--------------------+