Format MySQL date and convert to year-month-day


Let us first create a table −

mysql> create table DemoTable666(AdmissionDate varchar(200));
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable666 values('Sat,20 Jul 2019 04:29:35');
Query OK, 1 row affected (1.12 sec)
mysql> insert into DemoTable666 values('Fri,02 Oct 2018 12:19:15');
Query OK, 1 row affected (1.05 sec)
mysql> insert into DemoTable666 values('Sun,01 Aug 2016 11:10:05');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable666 values('Fri, 06 Nov 2015 04:06:05 -0500');
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable666;

This will produce the following output −

+---------------------------------+
| AdmissionDate                   |
+---------------------------------+
| Sat,20 Jul 2019 04:29:35        |
| Fri,02 Oct 2018 12:19:15        |
| Sun,01 Aug 2016 11:10:05        |
| Fri, 06 Nov 2015 04:06:05 -0500 |
+---------------------------------+
4 rows in set (0.00 sec)

Following is the query to convert the above date format to year-month-day −

mysql> select DATE_FORMAT(STR_TO_DATE(AdmissionDate, '%a, %d %b %Y %H:%i:%S'),'%Y-%m-%d %H:%i:%s') AS MyDate from DemoTable666;

This will produce the following output −

+---------------------+
| MyDate              |
+---------------------+
| 2019-07-20 04:29:35 |
| 2018-10-02 12:19:15 |
| 2016-08-01 11:10:05 |
| 2015-11-06 04:06:05 |
+---------------------+
4 rows in set, 1 warning (0.00 sec)
raja
Published on 23-Aug-2019 16:39:54
Advertisements