How to add a year and two days to a date with a single MySQL query?

MySQLMySQLi Database

For this, use INTERVAL in MySQL. Let us first create a table −

mysql> create table DemoTable1376
   -> (
   -> AdmissionDate date
   -> );
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1376 values('2018-01-21');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1376 values('2017-12-01');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1376 values('2018-11-02');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1376 values('2019-03-14');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement−

mysql> select * from DemoTable1376;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2018-01-21    |
| 2017-12-01    |
| 2018-11-02    |
| 2019-03-14    |
+---------------+
4 rows in set (0.00 sec)

Following is the query to add a year and two days to a date in MySQL −

mysql> select date_add(date_add(AdmissionDate,interval 1 year),interval 2 day) AS AfterAddingOneYearTwoDays from DemoTable1376;

This will produce the following output −

+---------------------------+
| AfterAddingOneYearTwoDays |
+---------------------------+
| 2019-01-23                |
| 2018-12-03                |
| 2019-11-04                |
| 2020-03-16                |
+---------------------------+
4 rows in set (0.00 sec)
raja
Published on 11-Nov-2019 09:32:34
Advertisements