Skip to main content
Log inGet a demo
Back to SQL Dictionary
Logical operators

SQL INTERVAL

What is SQL INTERVAL?

The SQL INTERVAL function is used to create an INTERVAL data type in SQL. An INTERVAL represents a period of time or duration. This function is typically used to specify a time interval or duration when working with date and time values in a structured and standardized format. It allows you to perform calculations, comparisons, and manipulations involving time-based values.

When you would use it

You would use the SQL INTERVAL function when you need to work with time intervals, durations, or periods within your SQL queries. INTERVALs are useful for a variety of applications, including calculating date and time differences, setting time-based constraints, and performing date arithmetic.

Syntax

The syntax for the SQL INTERVAL function is as follows:

INTERVAL value unit
  • value: The numeric value representing the duration of the interval.
  • unit: The unit of time for the interval, such as YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, etc.

Parameter values

  • value: A numeric value that specifies the length of the time interval.
  • unit: The unit of time for the interval, which can be any of the following: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, etc.

Example query

Suppose you have a table named "tasks" with columns "task_id," "start_date," and "end_date." You want to find tasks that have a duration of more than 5 days. Here's a SQL query using the INTERVAL function to achieve this:

SELECT task_id, start_date, end_date
FROM tasks
WHERE (end_date - start_date) > INTERVAL 5 DAY;

In the above query, we subtract the "start_date" from the "end_date" to calculate the duration of each task and then use the INTERVAL function to specify a duration of 5 days for comparison.

Example table response

Assuming the "tasks" table contains the following data:

| task_id | start_date           | end_date             |
| ------- | -------------------  | -------------------  |
| 1       | 2023-10-01           | 2023-10-10           |
| 2       | 2023-10-05           | 2023-10-08           |
| 3       | 2023-10-15           | 2023-10-16           |
| 4       | 2023-10-10           | 2023-10-13           |

The query mentioned earlier would return the following result:

| task_id | start_date           | end_date             |
| ------- | -------------------  | -------------------  |
| 1       | 2023-10-01           | 2023-10-10           |
| 4       | 2023-10-10           | 2023-10-13           |

This result shows tasks with durations of more than 5 days, as determined by the INTERVAL function.

Use cases

  • Calculating durations or time differences between two date and time values.
  • Setting time-based constraints or conditions in the database.
  • Managing and scheduling time-based events and tasks.

SQL languages this is available for

The SQL INTERVAL function is not part of the standard SQL specification but is available in specific relational database management systems (RDBMS) such as PostgreSQL and some versions of MySQL, each with its own syntax and variations. If you plan to use INTERVAL data types and the INTERVAL function in your SQL queries, consult the documentation of your specific database system for details on how to use them.

Related

SQL NANVL

SQL % Wildcard

SQL _ Wildcard

Share

Book a demo

Work email

Start syncing data in minutes

Check out this 5 minute interactive demo to learn how you can sync customer data to any business tool.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes