Name

STR_TO_DATE( )

Synopsis

STR_TO_DATE(datetime, 'format_code')

This function returns the date and time of a given string for a given format. The function takes a string containing a date or time, or both. So that the function may convert the string given, the formatting code for the string needs to be provided. The formatting codes are the same codes used by the DATE_FORMAT() function. This function is available as of Version 4.1.1 of MySQL.

 SELECT STR_TO_DATE('January 15, 2005 1:30 PM',
                    '%M %d, %Y %h:%i %p')
           AS Anniversary;
+---------------------+
| Anniversary         |
+---------------------+
| 2005-01-15 13:30:00 |
+---------------------+

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.