Updated May 24, 2023
Introduction to MySQL Interval
The MySQL Interval Operator uses the binary search algorithm to find the items from the list and returns the values in range 0 to N. It can also be said that it returns the index of the argument, which is greater than the first argument passed in the interval function.
It outputs 0(zeroth index) if 1st number is less than the 2nd number and 1(index 1) if 1st number is less than the 3rd number like this, it goes on and checks for the last element in the function, and if the first element is null, it will return -1 as an output.
All elements or arguments passed in the function acts as an integer.
Below is the syntax of MySQL interval function
select interval(n1,n2, n3…..nn)
If n<n1, then it returns 0. If n<n2, then it will return 1, n<n3, it will return 2, and so on. All the arguments must be in ascending order to function like n1<n2>n3……nn. Otherwise, it will not work properly.
The table below describes the unit associated with the interval function’s expression.
|DAY_MICROSECOND||‘DAYS HOURS:MINUTES:SECONDS. MICROSECONDS’|
Examples to Implement MySQL Interval
Below are the examples of implementing MySQL Interval:
Output: Since the first argument is null, the above query returns -1 as an output in the output console.
MySQL interval operator is mainly used for date and time calculations. The below expression is used to create an interval:
INTERVAL expression unit
The expression defines the interval value, and the unit determines the unit of the interval.
Example: interval 5 day
The above example explains how 5 days are created.
- Date + interval expression unit
- Date + interval expression unit
Generally, the interval values are calculated with date, time,date_add,date_subfunctions, etc.
The below statement adds four days to June 4, 2020that returns on June 8, 2020:
select '2020-06-04' + interval 4 day;
When an interval value is used in a date, and datetime expression that involved value and the interval value will be on the right-hand side of the expression, we can use the negative date and date-time value in the expression as shown in the below example:
select '2020-06-04' + interval -5 day;
select date_add('2020-06-04' , interval 2 month), date_sub('2020-06-04' , interval 2 month);
In the above query, two months are added on 2020-06-04 with the date_add() function, and then two months are subtracted from 2020-06-04 with the date_sub() function.
In the above query, timestampadd function uses three arguments unit, interval, and expression, which adds 45 minutes in the given date: 2020-06-04
Mysql interval keyword is also used with now() and curdate()
Below is the query that explains how the interval is used with the now() and curdate() function
select now() + interval 3 day;
In the above query, three days are added in the now function. Now function gives us the date plus the current time of the system.
select now() - interval 3 day;
In the above query, 3 days are subtracted from now.
select now() - interval 4 hour;
In the above query, 4 hours is subtracted from the current date and time. Therefore, the output will be in date and time format.
select curdate()-interval 3 day;
In the above query, 3 days is subtracted from the current date. Therefore, the output will be only the 3 day less than the current date.
select curdate() + interval 3 day;
In this article, we have learned about the interval function. We have also learned about how it is used with different MySQL functions like the date(), time(), date_add(), date_sub(), now(), curdate(), etc. In this article, we have tried to cover every concept of interval easily. In this article, all the concepts of the interval are explained with MySQL query along with the screenshot of the output console to provide a better understanding to the reader.
We hope that this EDUCBA information on “MySQL Interval” was beneficial to you. You can view EDUCBA’s recommended articles for more information.