How can I implement an interval condition for due dates correctly in MySQL?


Let us first create a table −

mysql> create table DemoTable (DueDate date);
Query OK, 0 rows affected (0.92 sec)

Example

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2019-01-12');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('2019-04-01');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('2019-07-19');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+------------+
| DueDate    |
+------------+
| 2019-01-12 |
| 2019-04-01 |
| 2019-07-19 |
+------------+
3 rows in set (0.00 sec)

Following is the query to implement an interval condition correctly for dates and increments the months by 5 for every value in the column −

mysql> update DemoTable set DueDate=date_add(DueDate,interval 5 MONTH);
Query OK, 3 rows affected (0.19 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable;

Output

+------------+
| DueDate    |
+------------+
| 2019-06-12 |
| 2019-09-01 |
| 2019-12-19 |
+------------+
3 rows in set (0.00 sec)

Updated on: 02-Jul-2020

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements