MySQL add days to a date?


To add days to a date, you can use DATE_ADD() function from MySQL. The syntax is as follows to add days to a date −

INSERT INTO yourTableName VALUES(DATE_ADD(now(),interval n day));

In the above syntax, you can use curdate() instead of now(). The curdate() will store only date while now() will store both date and time.

Here is the demo of both the functions. To understand the above syntax, let us create a table.

mysql> create table addingDaysDemo
   −> (
   −> yourDateTime datetime
   −> );
Query OK, 0 rows affected (1.09 sec)

Use both the above both functions now() and curdate() in insert statement and use “interval” to add days. The query to add days to a date is as follows −

mysql> insert into addingDaysDemo values(date_add(now(),interval 1 day));
Query OK, 1 row affected (0.14 sec)

mysql> insert into addingDaysDemo values(date_add(now(),interval 4 day));
Query OK, 1 row affected (0.17 sec)

mysql> insert into addingDaysDemo values(date_add(now(),interval 5 day));
Query OK, 1 row affected (0.14 sec)

mysql> insert into addingDaysDemo values(date_add(now(),interval 7 day));
Query OK, 1 row affected (0.17 sec)

mysql> insert into addingDaysDemo values(date_add(now(),interval 9 day));
Query OK, 1 row affected (0.15 sec)

mysql> insert into addingDaysDemo values(date_add(curdate(),interval 1 day));
Query OK, 1 row affected (0.18 sec)

mysql> insert into addingDaysDemo values(date_add(curdate(),interval 4 day));
Query OK, 1 row affected (0.16 sec)

mysql> insert into addingDaysDemo values(date_add(curdate(),interval 5 day));
Query OK, 1 row affected (0.17 sec)

mysql> insert into addingDaysDemo values(date_add(curdate(),interval 7 day));
Query OK, 1 row affected (0.12 sec)

mysql> insert into addingDaysDemo values(date_add(curdate(),interval 9 day));
Query OK, 1 row affected (0.15 sec)

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

mysql> select *from addingDaysDemo;

The following is the output −

+---------------------+
| yourDateTime        |
+---------------------+
| 2018-12-14 15:36:57 |
| 2018-12-17 15:37:06 |
| 2018-12-18 15:37:12 |
| 2018-12-20 15:37:17 |
| 2018-12-22 15:37:21 |
| 2018-12-14 00:00:00 |
| 2018-12-17 00:00:00 |
| 2018-12-18 00:00:00 |
| 2018-12-20 00:00:00 |
| 2018-12-22 00:00:00 |
+---------------------+
10 rows in set (0.00 sec)

Updated on: 30-Jul-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements