Flint Flint - 1 month ago 11
MySQL Question

Extract year from a string in mysql

STR_TO_DATE() returns null for this string "Apr 17 1989 12:00AM".

How to get the year from the string "Apr 17 1989 12:00AM" with regular expression in mysql?

Answer

Works for me...

mysql> SELECT STR_TO_DATE('Apr 17 1989 12:00AM','%M %d %Y %h:%i%p');
+-------------------------------------------------------+
| STR_TO_DATE('Apr 17 1989 12:00PM','%M %d %Y %h:%i%p') |
+-------------------------------------------------------+
| 1989-04-17 00:00:00                                   |
+-------------------------------------------------------+