How to add a day to datetime field in MySQL query?

MySQLMySQLi Database

To add a day to datetime field, use the DATE_ADD() function. The syntax is as follows −

SELECT DATE_ADD(yourColumnName,interval yourIntegerValue day) as anyVariableName from yourTableName;

Let us first create a table −

mysql> create table AddOneDayDemo
−> (
   −> YourDay datetime
−> );
Query OK, 0 rows affected (1.37 sec)

Insert current date with the help of curdate() and after that use date_add() function to add a day.

To insert a day into the table, the following is the query −

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

Display records with the help of select statement. The query is as follows −

mysql> select *from AddOneDayDemo;

The following is the record with current date −

| YourDay             |
+---------------------+
| 2018-11-27 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

The query to add a day to current date is as follows −

mysql> select date_add(YourDay,interval 1 day) as yourDayafteraddingoneday from AddOneDayDemo;

The following is the output -

+--------------------------+
| yourDayafteraddingoneday |
+--------------------------+
| 2018-11-28 00:00:00      |  
+--------------------------+
1 row in set (0.00 sec)

The above output displays a date that is an addition to the current date.

raja
Published on 10-Jan-2019 05:53:26
Advertisements