Adding a day to a DATETIME format value in MySQL?

MySQLMySQLi Database

To add a day to a DATETIME format value, you can use DATE_ADD() function from MySQL.

The syntax is as follows −

select date_add(now(),interval 1 day) as anyVariableName;

Now you can implement the above syntax in order to add a day to a datetime format.

mysql> select date_add(now(),interval 1 day) as Adding1DayDemo;

The following is the output −

+---------------------+
| Adding1DayDemo      |
+---------------------+
| 2018-12-07 20:06:59 |
+---------------------+
1 row in set (0.00 sec)

If you want to add a day only to a date, you can use curdate() function. The query is as follows −

mysql> select date_add(curdate(),interval 1 day) as Adding1DayDemo;

The following is the output −

+----------------+
| Adding1DayDemo |
+----------------+
| 2018-12-07     |
+----------------+
1 row in set (0.00 sec)

Let us create a table and insert some date values to it −

mysql> create table AddOneday
   −> (
   −> DueTime datetime
   −> );
Query OK, 0 rows affected (1.05 sec)

Now you can insert records with the help of both the functions now() and curdate().The query is as follows −

mysql> insert into AddOneday values(now());
Query OK, 1 row affected (0.22 sec)

mysql> insert into AddOneday values(curdate());
Query OK, 1 row affected (0.53 sec)

Display all records from the table with the help of select statement −

mysql> select *from AddOneday;

The following is the output −

+---------------------+
| DueTime             |
+---------------------+
| 2018-12-06 20:10:47 |
| 2018-12-06 00:00:00 |
+---------------------+
2 rows in set (0.00 sec)

Add a day with the help of DATE_ADD(). The query is as follows −

mysql> select date_add(DueTime,interval 1 day) as AddingOneDay from AddOneday;

The following is the output displaying the updated dates −

+---------------------+
| AddingOneDay        |
+---------------------+
| 2018-12-07 20:10:47 |
| 2018-12-07 00:00:00 |
+---------------------+
2 rows in set (0.00 sec)
raja
Published on 11-Jan-2019 12:41:02
Advertisements