How do I re-format datetime in MySQL?

MySQLMySQLi Database

To re-format datetime in MySQL, you can use DATE_FORMAT(). MySQL gives in the format yyyy-mm-dd.

Let us first create a table −

mysql> create table DemoTable1558
   -> (
   -> EmployeeJoiningDate datetime
   -> );
Query OK, 0 rows affected (1.10 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1558 values(CURDATE());
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1558 values(NOW());
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1558 values('2018-01-10');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1558 values('2017-12-31');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1558;

This will produce the following output −

+---------------------+
| EmployeeJoiningDate |
+---------------------+
| 2019-10-13 00:00:00 |
| 2019-10-13 17:55:33 |
| 2018-01-10 00:00:00 |
| 2017-12-31 00:00:00 |
+---------------------+
4 rows in set (0.00 sec)

Following is the query to re-format date time in MySQL −

mysql> select date_format(EmployeeJoiningDate,'%d-%m-%Y') as changeFormat from DemoTable1558;

This will produce the following output −

+--------------+
| changeFormat |
+--------------+
| 13-10-2019   |
| 13-10-2019   |
| 10-01-2018   |
| 31-12-2017   |
+--------------+
4 rows in set (0.00 sec)
raja
Published on 12-Dec-2019 11:11:06
Advertisements