I'm running a simple procedure that reads from a VARCHAR
column and inserts data into a TIME
column, using this format:
STR_TO_DATE(vTestTime, '%H:%i').
I'm getting the error
SQL Error (1411): Incorrect datetime value: '09:22' for function str_to_date
I find that a similar statement, SELECT STR_TO_DATE("09:22", "%H:%i")
gives me a NULL when I run it from a HeidiSQL client.
I'm sure this syntax used to work and I'm not aware of having changed any settings, can you suggest what I've done wrong?
Edit: Just remembered I have made a recent change, I installed a WordPress database on the same server, might that have had some affect?
Here's what get running on the server:
mysql> select str_to_date("09:22", "%H:%i");
+-------------------------------+
| str_to_date("09:22", "%H:%i") |
+-------------------------------+
| NULL |
+-------------------------------+
1 row in set, 1 warning (0.00 sec)
Warning (Code 1411): Incorrect datetime value: '09:22' for function str_to_date
Best Answer
This looks like a bug (though it isn't technically a bug, see my first edit towards the end of the answer):
I see the behaviour on MySQL 5.7.20 and 5.7.21 (on Centos 7.4 if it matters):
And the warning is code 1411
Incorrect datetime value: '09:22' for function str_to_date
.ANSI_QUOTES is not enabled, and I get the same warning if I use single-quotes.
From what I understand reading the documentation, the query should work:
This query, containing a date as well as a time part, does work:
Running the original query on MariaDB 10.2.14 works correctly:
EDIT: This was reported as a bug previously, see here: https://bugs.mysql.com/bug.php?id=80064
These modes are enabled by default in MySQL 5.7 (but not in MariaDB 10.2). So, after removing these from the sql_mode system variable, we get the desired result:
EDIT 2: We don't want to accidentally remove helpful/important modes from the
sql_mode
system variable, so ideally we should only remove specific modes, in this case NO_ZERO_DATE and NO_ZERO_IN_DATE. This can be done this way:(Use
SET GLOBAL sql_mode...
if you want to change it for the global scope rather than just the session.)If you want to set
sql_mode
in the MySQL config file, then I think you'll just have to "hard-code" it, i.e. just find out what the default modes are for the version you're using, remove the undesired modes, and add the resulting string e.g. to the[mysqld]
section of the .cnf file:This should be correct for MySQL 5.7. When upgrading to the next version, you'll have to update the setting again.
Note that stored procedures run with their own
sql_mode
s:Source: the MySQL stored procedure documentation.
You can also change the
sql_mode
within a stored procedure.