What MySQL returns on passing an invalid string as an argument to STR_TO_DATE() function?


If we pass an invalid string as an argument to STR_TO_DATE() function then MySQL will return NULL as output along with a warning. Following is an example to understand the same −

mysql> Select STR_TO_DATE('20173210', '%Y%d%m');
+-----------------------------------+
| STR_TO_DATE('20173210', '%Y%d%m') |
+-----------------------------------+
| NULL                              |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)

In the query above the string value is invalid because of wrong (32) day value. Hence it returns NULL values and a warning which is given below.

mysql> Show warnings\G
*************************** 1. row ***************************
   Level: Warning
      Code: 1411
Message: Incorrect datetime value: '20173210' for function str_to_date
1 row in set (0.00 sec)

Updated on: 30-Jan-2020

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements