Converting a date in MySQL from string field?

MySQLMySQLi Database

To convert string to date in MySQL, you can use STR_TO_DATE() function. The syntax is as follows −

select str_to_date(‘StringValue’, '%d,%m,%Y') as anyVariableName;

Apply the above syntax in the following query wherein, we have a string value −

mysql> SELECT STR_TO_DATE('26,11,2018','%d,%m,%Y');

The following is the output −

+--------------------------------------+
| STR_TO_DATE('26,11,2018','%d,%m,%Y') |
+--------------------------------------+
| 2018-11-26                           |
+--------------------------------------+
1 row in set (0.00 sec)

Let us see another example, to change the format of the date −

SELECT STR_TO_DATE("November 26 2018", "%M %d %Y");

Sample Output

+---------------------------------------------+
| STR_TO_DATE("November 26 2018", "%M %d %Y") |
+---------------------------------------------+
| 2018-11-26                                  |
+---------------------------------------------+
1 row in set (0.03 sec)
raja
Published on 24-Dec-2018 11:54:47
Advertisements