Add 30 days to a value in a MySQL table?

MySQLMySQLi Database

To add 30 days to a value in the table, you can use ADDDATE() function with UPDATE command. The syntax is as follows:

UPDATE yourTableName
SET yourDateColumnName=ADDDATE(yourDateColumnName,INTERVAL 30 DAY);

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table Add30DayDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> ShippingDate date,
   -> PRIMARY KEY(ID)
   -> );
Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into Add30DayDemo(ShippingDate) values('2019-02-04');
Query OK, 1 row affected (0.20 sec)
mysql> insert into Add30DayDemo(ShippingDate) values('2018-11-15');
Query OK, 1 row affected (0.18 sec)
mysql> insert into Add30DayDemo(ShippingDate) values('2013-05-18');
Query OK, 1 row affected (0.15 sec)
mysql> insert into Add30DayDemo(ShippingDate) values('2017-08-25');
Query OK, 1 row affected (0.20 sec)
mysql> insert into Add30DayDemo(ShippingDate) values('2016-03-01');
Query OK, 1 row affected (0.12 sec)
mysql> insert into Add30DayDemo(ShippingDate) values('2015-09-03');
Query OK, 1 row affected (0.13 sec)

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

mysql> select *from Add30DayDemo;

The following is the output:

+----+--------------+
| Id | ShippingDate |
+----+--------------+
|  1 | 2019-02-04   |
|  2 | 2018-11-15   |
|  3 | 2013-05-18   |
|  4 | 2017-08-25   |
|  5 | 2016-03-01   |
|  6 | 2015-09-03   |
+----+--------------+
6 rows in set (0.00 sec)

Here is the query to add 30 days value in a table using DATEADD():

mysql> update Add30DayDemo
   -> SET ShippingDate=ADDDATE(ShippingDate,INTERVAL 30 DAY);
Query OK, 6 rows affected (0.44 sec)
Rows matched: 6 Changed: 6 Warnings: 0

Now check the table records once again. The query is as follows:

mysql> select *from Add30DayDemo;

The following is the output with the updated date:

+----+--------------+
| Id | ShippingDate |
+----+--------------+
|  1 | 2019-03-06   |
|  2 | 2018-12-15   |
|  3 | 2013-06-17   |
|  4 | 2017-09-24   |
|  5 | 2016-03-31   |
|  6 | 2015-10-03   |
+----+--------------+
6 rows in set (0.00 sec)
raja
Published on 25-Feb-2019 14:11:41
Advertisements