STR_TO_DATE() function of MySQL

user1369905 picture user1369905 · Sep 7, 2012 · Viewed 18k times · Source

I have date in this format 5 Mar 1985 0:00 stored in table as VARCHAR.
I want to convert it into Datetime, I am using the STR_TO_DATE() as follows:

SELECT STR_TO_DATE(birth_date, '%d %m %Y %h:%i') FROM student WHERE pk = 29

But it returns NULL.

Answer

Devart picture Devart · Sep 7, 2012

Try this format - '%e %b %Y %k:%i', for example -

SELECT STR_TO_DATE('5 Mar 1985 2:33', '%e %b %Y %k:%i') dt;
+---------------------+
| dt                  |
+---------------------+
| 1985-03-05 02:33:00 |
+---------------------+