MySQL 5.7 STR_TO_DATE

ⅰ亾dé卋堺 提交于 2019-12-20 07:14:51

问题


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

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)

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?


回答1:


As it says in the manual,

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

That causes the result to be NULL in cases such like this, because your partial “date” contains zero values.



来源:https://stackoverflow.com/questions/39223080/mysql-5-7-str-to-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!