How can I use MySQL INTERVAL keyword while extracting the part of the date?


With the help of following example we can understand that how we can use MySQL INTERVAL keyword with EXTRACT() function −

mysql> Select StudentName, RegDate, EXTRACT(YEAR from RegDate+INTERVAL 2 year) AS 'Two Year Interval' from testing where StudentName = 'Gaurav';
+-------------+---------------------+-------------------+
| StudentName | RegDate             | Two Year Interval |
+-------------+---------------------+-------------------+
| Gaurav      | 2017-10-29 08:48:33 |             2019  |
+-------------+---------------------+-------------------+
1 row in set (0.02 sec)

Above query is showing how we can use INTERVAL keyword with EXTRACT() function used in MySQL table query.

mysql> Select EXTRACT(Year from '2017-10-22 05:03:45' + INTERVAL 2 Year) AS 'Two Year Interval';
+-------------------+
| Two Year Interval |
+-------------------+
|              2019 |
+-------------------+
1 row in set (0.00 sec)

Above query is showing how we can use INTERVAL keyword with EXTRACT() function used for particular given date.

Updated on: 29-Jan-2020

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements