STR_TO_DATE( )

This MySQL function returns the date and time of a given string for a given format.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

STR_TO_DATE(datetime, 'format_code')

Explanation

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.

Examples

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.