MySQL 5.7 STR_TO_DATE [英] MySQL 5.7 STR_TO_DATE
问题描述
我正在运行 MySQL 5.7 并希望将一些字符串转换为日期.我在这里参考了手册:http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
I am running MySQL 5.7 and wanted to convert some strings to date. I referred the manual here: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
并尝试了以下(请注意MySQL版本)
And tried out the following(please note the MySQL version)
mysql> SELECT STR_TO_DATE('9','%m');
+-----------------------+
| STR_TO_DATE('9','%m') |
+-----------------------+
| NULL |
+-----------------------+
1 row in set, 1 warning (0.00 sec)
mysql> show warnings;
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------+
| Warning | 1411 | Incorrect datetime value: '9' for function str_to_date |
+---------+------+--------------------------------------------------------+
1 row in set (0.00 sec)
但是从 MySQL 5.7 的在线手册中,我应该看到以下内容:
But from the online manual for MySQL 5.7, I should see the following:
mysql> SELECT STR_TO_DATE('9','%m');
-> '0000-09-00'
是否有什么我遗漏的地方,或者这可能是一个错误?
Is there something that I'm missing, or could this be a bug?
推荐答案
正如手册中所说,
如果启用了 NO_ZERO_DATE 或 NO_ZERO_IN_DATE SQL 模式,则不允许使用零日期或部分日期.在这种情况下,STR_TO_DATE() 返回 NULL 并生成警告
If the NO_ZERO_DATE or NO_ZERO_IN_DATE SQL mode is enabled, zero dates or part of dates are disallowed. In that case, STR_TO_DATE() returns NULL and generates a warning
在这种情况下,这会导致结果为 NULL,因为您的部分日期"包含零值.
That causes the result to be NULL in cases such like this, because your partial "date" contains zero values.
这篇关于MySQL 5.7 STR_TO_DATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!