How to add multiple intervals to DATE_ADD() in MySQL?

MySQLMySQLi Database

The current date and time is as follows −

mysql> select now();

Output

This will produce the following output −

+---------------------+
| now()               |
+---------------------+
| 2019-06-15 12:24:06 |
+---------------------+
1 row in set (0.00 sec)

Let us first create a table −

mysql> create table DemoTable
   ->(
   -> ArrivalDate datetime
   -> );
Query OK, 0 rows affected (1.15 sec)

Insert some records in the table using insert command. Here, we are adding multiple intervals to the DATE_ADD() method −

mysql> insert into DemoTable values(DATE_ADD(DATE_ADD(NOW(), INTERVAL 6
MONTH), INTERVAL 1 YEAR));
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values(DATE_ADD(DATE_ADD(NOW(), INTERVAL 3
MONTH), INTERVAL 4 YEAR));
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+---------------------+
| ArrivalDate         |
+---------------------+
| 2020-12-15 12:23:39 |
| 2023-09-15 12:23:48 |
+---------------------+
2 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 14:01:42
Advertisements