How to update all dates in a Table?

MySQLMySQLi Database

You can use UPDATE with DATE_ADD() to update all dates. Let us first create a table −

mysql> create table DemoTable
   (
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientProjectDueDate date
   );
Query OK, 0 rows affected (1.19 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable(ClientProjectDueDate) values('2018-01-21');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(ClientProjectDueDate) values('2019-03-25');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(ClientProjectDueDate) values('2013-11-01');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(ClientProjectDueDate) values('2015-06-14');
Query OK, 1 row affected (0.23 sec)

Display all records from the table using a select statement. The query is as follows −

mysql> select * from DemoTable;

This will produce the following output −

+----------+----------------------+
| ClientId | ClientProjectDueDate |
+----------+----------------------+
| 1        | 2018-01-21           |
| 2        | 2019-03-25           |
| 3        | 2013-11-01           |
| 4        | 2015-06-14           |
+----------+----------------------+
4 rows in set (0.00 sec)

Following is the query to update all dates in a table −

mysql> update DemoTable set ClientProjectDueDate=date_add(ClientProjectDueDate,INTERVAL 2 YEAR);
Query OK, 4 rows affected (0.71 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us display all records from the table once again −

mysql> select * from DemoTable;

This will produce the following output −

+----------+----------------------+
| ClientId | ClientProjectDueDate |
+----------+----------------------+
| 1        | 2020-01-21           |
| 2        | 2021-03-25           |
| 3        | 2015-11-01           |
| 4        | 2017-06-14           |
+----------+----------------------+
4 rows in set (0.00 sec)
raja
Published on 03-May-2019 11:04:17
Advertisements