w3resource

MySQL INTERVAL() function

INTERVAL() function

MySQL INTERVAL() function returns the index of the argument that is more than the first argument.

This function is useful in -

  • INTERVAL() can be used to adjust dates by adding or subtracting a specific duration.
  • It's particularly useful for dealing with durations, which represent a span of time.
  • In analytics and reporting, INTERVAL() is used to group data into time intervals.
  • When querying a database for records within a specific date range, INTERVAL() simplifies this process.
  • It can assist in handling time zones by allowing you to adjust date and time values relative to a specific time zone.
  • When combined with scheduling tools or scripts, INTERVAL() can be used to automate tasks that need to occur at regular intervals.
  • In forecasting models and trend analysis, INTERVAL() helps in projecting future events based on historical data.

Syntax:

INTERVAL(N,N1,N2,N3,...)

It returns 0 if 1st number is less than the 2nd number and 1 if 1st number is less than the 3rd number and so on or -1 if 1st number is NULL.

All arguments are treated as an integer.

MySQL Version: 8.0

Example: MySQL INTERVAL() function

The following MySQL statement the 1st number is 85, which is less than 175. All other numbers in the argument are smaller than 85. So, it returns the index of 175. Notice that indexing is started with the 2nd number and first position is zero.

Code:

SELECT INTERVAL(85, 1, 75, 17, 30,56, 175);

Output:

mysql> SELECT INTERVAL(85, 1, 75, 17, 30,56, 175);
+-------------------------------------+
| INTERVAL(85, 1, 75, 17, 30,56, 175) |
+-------------------------------------+
|                                   5 | 
+-------------------------------------+
1 row in set (0.02 sec)

Slideshow of MySQL Comparison Function and Operators

Previous: IN()
Next: IS NOT NULL



Follow us on Facebook and Twitter for latest update.