What is INTERVAL data type?

The INTERVAL data type is used in SQL to represent a span of time. It can be used to store a duration that can be added to or subtracted from a timestamp. The INTERVAL type is particularly useful for calculations involving time, such as determining the difference between two dates or adding a specific duration to a date.

Key Features:

  • Components: An INTERVAL can represent a duration in terms of years, months, days, hours, minutes, and seconds.
  • Syntax: The syntax for defining an INTERVAL can vary slightly between SQL databases, but a common format is:
    INTERVAL 'value unit'
    Where value is a number and unit can be one of the following: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND.

Example Usage:

  1. Creating an Interval:

    SELECT INTERVAL '1 DAY' AS one_day;
  2. Adding an Interval to a Timestamp:

    SELECT CURRENT_TIMESTAMP + INTERVAL '2 HOURS' AS new_time;
  3. Calculating the Difference Between Two Dates:

    SELECT end_date - start_date AS duration
    FROM your_table;

Important Notes:

  • The exact syntax and capabilities of the INTERVAL type may vary between different SQL databases (like PostgreSQL, MySQL, Oracle, etc.), so it's important to refer to the specific documentation for your database system for details on how to use it effectively.

0 Comments

no data
Be the first to share your comment!