MySQL STR_TO_DATE Function

MySQL STR_TO_DATE function is a Date method, which acts as an inverse of the DATE_FORMAT function. This STR_TO_DATE function takes a string as an input and converts the string to Date, DateTime, or Time values based on the format given by the user. The basic syntax of it is as shown below:

STR_TO_DATE(string_expression, format);

MySQL STR_TO_DATE Function Example

Here, We used the STR_TO_DATE to convert the different string formats to Date. For this MySQL query, we use %d %m and %Y formats.

SELECT STR_TO_DATE('31 12 2019', '%d %m %Y');

SELECT STR_TO_DATE('31,12,2019', '%d,%m,%Y');

SELECT STR_TO_DATE('12,2019,31', '%m,%Y,%d');
Example 1

This is another Date method example of this one with different formats.

SELECT STR_TO_DATE('12 Jan, 2019', '%d %b, %Y');

SELECT STR_TO_DATE('2nd Jan, 2019', '%D %b, %Y');

SELECT STR_TO_DATE('2nd January, 2019', '%D %M, %Y');
STR_TO_DATE Example 2