How to convert a string to date in MySQL?

We can convert a string to date with the help of STR_TO_DATE() function. Let us see an example.

Creating a table.

mysql> create table StringToDateDemo
   -> (
   -> YourDate varchar(100)
   -> );
Query OK, 0 rows affected (0.49 sec)

Inserting records into the table.

mysql>  insert into StringToDateDemo values('10/27/2018');
Query OK, 1 row affected (0.11 sec)

The following is the syntax to convert string to date using the STR_TO_DATE() function.

SELECT  STR_TO_DATE(yourColumnName, '%m/%d/%Y')
   from yourTableName;

Let us now implement it.

mysql>SELECT  STR_TO_DATE(YourDate, '%m/%d/%Y')
   -> from StringToDateDemo;

Here is the output.

| STR_TO_DATE(YourDate, '%m/%d/%Y') |
| 2018-10-27                        |
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started