Format date while inserting records in MySQL

MySQLMySQLi Database

To format date while inserting records, use DATE_FORMAT() in the MySQL INSERT statement. Let us first create a table −

mysql> create table DemoTable2012
   -> (
   -> ShippingDate varchar(20)
   -> );
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2012 values(date_format(curdate(),'%d.%m.%Y'));
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable2012 values(date_format(now(),'%d.%m.%Y'));
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable2012 values(date_format('2014-01-21','%d.%m.%Y'));
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable2012;

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| 21.12.2019 |
| 21.12.2019 |
| 21.01.2014 |
+--------------+
3 rows in set (0.00 sec)
raja
Published on 06-Apr-2020 16:19:33
Advertisements