WikiGalaxy

Personalize

Understanding PostgreSQL Interval

What is PostgreSQL Interval?

In PostgreSQL, the INTERVAL type is used to store a span of time. It is versatile and allows for the representation of time differences in terms of years, months, days, hours, minutes, and seconds.

Syntax and Usage:

The INTERVAL type can be used to add or subtract time from DATE or TIMESTAMP values. It can be defined using the INTERVAL keyword followed by a string indicating the time span.

Basic Example:

Here is a basic example of using the INTERVAL type in a query:


SELECT NOW() + INTERVAL '1 year 2 months 3 days';
    

The INTERVAL value can be customized to fit different requirements for date manipulation.

Console Output:

2024-12-28 14:45:00

Using INTERVAL with DATE

Adding INTERVAL to DATE:

The INTERVAL type can be used to perform arithmetic operations with DATE values, allowing for easy manipulation of date-based data.

Example of Adding Days:

Add 10 days to a specific date using the INTERVAL type:


SELECT DATE '2023-01-01' + INTERVAL '10 days';
    

The result will be a new date that reflects the addition of the specified interval.

Console Output:

2023-01-11

Subtracting INTERVAL from TIMESTAMP

Subtracting Time Intervals:

INTERVAL can also be used to subtract time spans from TIMESTAMP values, which is useful for calculating past dates and times.

Example of Subtracting Months:

Subtract 3 months from a specific timestamp:


SELECT TIMESTAMP '2023-06-15 10:00:00' - INTERVAL '3 months';
    

The result will be a new timestamp reflecting the subtraction of the specified interval.

Console Output:

2023-03-15 10:00:00

Combining INTERVALs

Combining Multiple INTERVALs:

PostgreSQL allows combining multiple INTERVALs to create complex time spans, which can be useful for advanced date calculations.

Example of Combining INTERVALs:

Combine 1 year, 2 months, and 10 days into a single INTERVAL:


SELECT INTERVAL '1 year' + INTERVAL '2 months' + INTERVAL '10 days';
    

The combined INTERVAL can then be used in further date or timestamp calculations.

Console Output:

1 year 2 mons 10 days

INTERVAL with EXTRACT Function

Using EXTRACT with INTERVAL:

The EXTRACT function can be used with INTERVAL to retrieve specific components like years, months, or days from a time span.

Example of Extracting Days:

Extract the number of days from an INTERVAL:


SELECT EXTRACT(DAY FROM INTERVAL '1 year 2 months 10 days');
    

The EXTRACT function can be applied to other components such as months or years.

Console Output:

10

INTERVAL Arithmetic

Performing Arithmetic with INTERVALs:

PostgreSQL supports arithmetic operations directly on INTERVALs, allowing for addition, subtraction, multiplication, and division.

Example of Multiplying INTERVAL:

Multiply an INTERVAL by a factor to scale the time span:


SELECT INTERVAL '10 days' * 2;
    

The resulting INTERVAL will be scaled accordingly, in this case doubling the number of days.

Console Output:

20 days

Casting INTERVAL to Other Types

Casting INTERVALs:

In PostgreSQL, INTERVALs can be cast to other types such as strings, allowing for flexible data manipulation and formatting.

Example of Casting to String:

Cast an INTERVAL to a string to format the output:


SELECT CAST(INTERVAL '1 year 2 months' AS VARCHAR);
    

The resulting string can be used in applications where text formatting is required.

Console Output:

1 year 2 mons

INTERVAL Formatting Options

Formatting INTERVAL Output:

PostgreSQL provides various formatting options for INTERVALs to customize their display according to user preferences.

Example of Custom Formatting:

Format an INTERVAL to display in a custom format:


SELECT TO_CHAR(INTERVAL '1 year 2 months 3 days', 'YYYY "years" MM "months" DD "days"');
    

The output can be tailored to meet specific display requirements, enhancing readability.

Console Output:

1 years 02 months 03 days

INTERVAL in Function Arguments

Using INTERVALs in Functions:

INTERVALs can be passed as arguments to functions, enabling dynamic date and time calculations based on input parameters.

Example with Custom Function:

Create a function that takes an INTERVAL and adds it to the current date:


CREATE FUNCTION add_interval_to_now(interval_value INTERVAL) RETURNS TIMESTAMP AS $$
BEGIN
  RETURN NOW() + interval_value;
END;
$$ LANGUAGE plpgsql;

SELECT add_interval_to_now(INTERVAL '5 days');
    

The function dynamically calculates a future date by adding the INTERVAL to the current date.

Console Output:

2023-10-23 14:45:00

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025