What MySQL returns if specified format string is not as per accordance with the date string passed as arguments to STR_TO_DATE() function?


If the specified format string and date string did not match then MySQL will return NULL value as output along with a warning. Following is an example to understand the same −

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

The query above returns NULL as output because the format string is having a comma (,) after %Y but date string is not having any comma after 2017.

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

Similarly, on distinguishing the order of date units in format string from the date string, MySQL will perform same as above. An example is given below to understand it −

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

In the above query, the order of units in the format string is changed from the order of units in the date string.

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

Updated on: 30-Jan-2020

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements