How to change the date in a table with date records with MySQL?

MySQLMySQLi Database

Let’s say you need to change the date and add years. For this, use UPDATE command along with DATE_ADD(). Let us first create a table −

mysql> create table DemoTable1984
   (
   ShippingDate date
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1984 values('2014-01-11');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1984 values('2015-12-23');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1984 values('2017-10-31');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1984 values('2018-06-01');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1984;

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| 2014-01-11   |
| 2015-12-23   |
| 2017-10-31   |
| 2018-06-01   |
+--------------+
4 rows in set (0.00 sec)

Here is the query to change the date −

mysql> update DemoTable1984 set ShippingDate=date_add(ShippingDate,interval 2 year);
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4 Warnings: 0

Let us check table records once again −

mysql> select * from DemoTable1984;

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| 2016-01-11   |
| 2017-12-23   |
| 2019-10-31   |
| 2020-06-01   |
+--------------+
4 rows in set (0.00 sec)
raja
Published on 31-Dec-2019 08:09:01
Advertisements