How to convert MM/YY to YYYY-MM-DD in MYSQL?

MySQLMySQLi Database

To convert, use str_to_date() in MySQL

Let us create a table and add date records −

Example

mysql> create table demo72
   -> (
   -> due_date varchar(40)
   -> );
Query OK, 0 rows affected (2.96 sec)

Insert some records into the table with the help of insert command −

Example

mysql> insert into demo72 values("11/15");
Query OK, 1 row affected (0.26 sec)

mysql> insert into demo72 values("02/20");
Query OK, 1 row affected (0.09 sec)

mysql> insert into demo72 values("07/95");
Query OK, 1 row affected (0.15 sec)

Display records from the table using select statement −

Example

mysql> select *from demo72;

This will produce the following output −

Output

+----------+
| due_date |
+----------+
| 11/15    |
| 02/20    |
| 07/95    |
+----------+
3 rows in set (0.00 sec)

Following is the query to convert MM/YY to YYYY-MM-DD in MySQL.

Example

mysql> select str_to_date(concat('10/', due_date), '%d/%m/%y') as original_date
-> from demo72;

This will produce the following output −

Output

+---------------+
| original_date |
+---------------+
| 2015-11-10    |
| 2020-02-10    |
| 1995-07-10    |
+---------------+
3 rows in set (0.00 sec)
raja
Published on 11-Dec-2020 05:16:41
Advertisements