How can I use the arithmetic operators (+,-,*,/) with unit values of INTERVAL keyword in MySQL?

MySQLMySQLi Database

We can use arithmetic operators (+,-,*, /) with the unit values of INTERVAL keyword as follows −

Use of Addition (+)

mysql> Select date('2017-10-22' + INTERVAL 2+2 Year) AS 'Date After (2+2)Years';
+------------------------+
| Date After (2+2) Years |
+------------------------+
| 2021-10-22             |
+------------------------+
1 row in set (0.00 sec)

Use of Subtraction (-)

mysql> Select date('2017-10-22' + INTERVAL 2-2 Year) AS 'Date After (2-2)Years';
+------------------------+
| Date After (2-2) Years |
+------------------------+
| 2017-10-22             |
+------------------------+
1 row in set (0.00 sec)

Use of Multiplication (*)

mysql> Select date('2017-10-22' + INTERVAL 2*2 Year) AS 'Date After (2*2)Years';
+------------------------+
| Date After (2*2) Years |
+------------------------+
| 2021-10-22             |
+------------------------+
1 row in set (0.00 sec)

Use of division (/)

mysql> Select date('2017-10-22' + INTERVAL 2/2 Year) AS 'Date After (2/2) Years';
+------------------------+
| Date After (2/2) Years |
+------------------------+
| 2018-10-22             |
+------------------------+
1 row in set (0.00 sec)
raja
Published on 19-Feb-2018 15:29:09
Advertisements