问题
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