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. To specify the format of the string returned, a formatting code needs to be provided in the second argument. The formatting codes are the same codes used by the DATE_FORMAT() function; see its definition for a list of those formats. This function is available as of version 4.1.1 of MySQL. Here is an example:

SELECT STR_TO_DATE(
'January 15, 2008 1:30 PM', 
'%M %d, %Y %h:%i %p'
) AS Anniversary;

+---------------------+
| Anniversary         |
+---------------------+
| 2008-01-15 13:30:00 |
+---------------------+

To retrieve a return value suitable for insertion into a date or time column, use '%Y-%m-%d' for a date column and '%h:%i:%s' for a time column.

Get MySQL in a Nutshell, 2nd Edition 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.