Skip to main content
St Louis

Back to all posts

How to Declare A Variable Interval In PostgreSQL?

Published on
4 min read
How to Declare A Variable Interval In PostgreSQL? image

Best PostgreSQL Guides to Buy in October 2025

1 PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database

PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database

BUY & SAVE
$35.23 $44.99
Save 22%
PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database
2 Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16

Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16

BUY & SAVE
$44.99
Learn PostgreSQL: Use, manage, and build secure and scalable databases with PostgreSQL 16
3 PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices

PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices

BUY & SAVE
$34.91 $54.99
Save 37%
PostgreSQL 16 Administration Cookbook: Solve real-world Database Administration challenges with 180+ practical recipes and best practices
4 PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)

PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)

BUY & SAVE
$51.32
PostgreSQL DBA (v17, v16, v15, v14, v13) - 2025 2nd Edition: Full PostgreSQL Database Administrator's Guide, Secret DBA skills, High Availability, ... (GitHub link provided) (PostgreSQL 17)
5 PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries

PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries

BUY & SAVE
$43.58 $49.99
Save 13%
PostgreSQL Query Optimization: The Ultimate Guide to Building Efficient Queries
6 Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications

Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications

BUY & SAVE
$35.99 $61.99
Save 42%
Mastering PostgreSQL 15: Advanced techniques to build and manage scalable, reliable, and fault-tolerant database applications
7 Introduction to PostgreSQL for the data professional.

Introduction to PostgreSQL for the data professional.

BUY & SAVE
$24.99
Introduction to PostgreSQL for the data professional.
8 Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)

Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)

  • AFFORDABLE PRICES FOR QUALITY USED BOOKS IN GOOD CONDITION.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY BUYING USED BOOKS.
  • THOROUGHLY VETTED: EACH BOOK ENSURES QUALITY AND DURABILITY.
BUY & SAVE
$41.94 $89.99
Save 53%
Beginning Databases with PostgreSQL: From Novice to Professional (Beginning From Novice to Professional)
9 Practical SQL, 2nd Edition: A Beginner's Guide to Storytelling with Data

Practical SQL, 2nd Edition: A Beginner's Guide to Storytelling with Data

BUY & SAVE
$25.48 $39.99
Save 36%
Practical SQL, 2nd Edition: A Beginner's Guide to Storytelling with Data
10 Mastering PostgreSQL 17: Elevate your database skills with advanced deployment, optimization, and security strategies

Mastering PostgreSQL 17: Elevate your database skills with advanced deployment, optimization, and security strategies

BUY & SAVE
$31.91
Mastering PostgreSQL 17: Elevate your database skills with advanced deployment, optimization, and security strategies
+
ONE MORE?

To declare a variable interval in PostgreSQL, you can follow these steps:

  1. Start by opening the PostgreSQL command-line interface or any other client application that allows you to execute SQL queries.
  2. To declare a variable, you need to use the DECLARE keyword followed by the variable name and data type. In this case, the data type should be "interval". For example, you can declare a variable named "my_interval" of type interval as follows: DECLARE my_interval INTERVAL;
  3. After declaring the variable, you can assign a specific value to it using the SET keyword, followed by the variable name and the desired interval value. The interval value can be given in various formats, such as 'HH:MI:SS', 'DAYS HH:MI:SS', or a numerical value followed by one of the interval units like 'hour', 'day', 'week', etc. For example: SET my_interval = '1 day'; The above code assigns a value of one day to the "my_interval" variable.
  4. You can also perform calculations or operations with interval variables. For example, you can add or subtract another interval from the variable using the INTERVAL keyword. Here's an example of adding 2 hours to the "my_interval" variable: SET my_interval = my_interval + INTERVAL '2 hours';
  5. To use the value stored in the interval variable in other parts of your SQL query, you can simply reference it by its name. For example: SELECT * FROM some_table WHERE some_column < now() - my_interval; In the above query, the "my_interval" variable is used to subtract the interval value from the current timestamp to filter the results.

Remember to end each statement with a semicolon (;) to execute it in PostgreSQL.

These steps demonstrate the basic process of declaring and using a variable interval in PostgreSQL.

How to calculate the sum of variable intervals within a specific range of dates in PostgreSQL?

To calculate the sum of variable intervals within a specific range of dates in PostgreSQL, you can use the SUM() function along with the FILTER clause. Here's an example query:

SELECT SUM(interval_column) FILTER (WHERE date_column >= 'start_date' AND date_column <= 'end_date') AS total_sum FROM your_table;

Replace interval_column with the name of the column containing intervals and date_column with the name of the column containing the dates. Also, replace 'start_date' and 'end_date' with the specific range of dates you want to consider.

For example, if you have a table named sales with columns interval_column containing intervals and date_column containing dates, and you want to calculate the sum of intervals between '2022-01-01' and '2022-01-31', the query will be:

SELECT SUM(interval_column) FILTER (WHERE date_column >= '2022-01-01' AND date_column <= '2022-01-31') AS total_sum FROM sales;

This query will return the sum of intervals within the specified date range.

What is the maximum precision for a variable interval in PostgreSQL?

In PostgreSQL, the maximum precision for a variable interval is 6 decimal places. The fractional seconds precision of an interval value can range from 0 to 6.

How to round a variable interval to a specific precision in PostgreSQL?

To round a variable interval to a specific precision in PostgreSQL, you can use the CAST function combined with the ROUND function.

Here is an example of how you can round a variable interval to a specific precision (in this case, 2 decimal places):

SELECT INTERVAL '1 day 2 hours 30 minutes' AS original_interval, CAST(CAST(ROUND(EXTRACT(epoch FROM INTERVAL '1 day 2 hours 30 minutes')::numeric / 60) AS numeric) * 60 AS interval) AS rounded_interval;

In this example, we are rounding the interval 1 day 2 hours 30 minutes to the precision of minutes. The EXTRACT(epoch FROM INTERVAL) function converts the interval to seconds, which is then divided by 60 to convert it to minutes. The CAST function is used to convert the result to numeric, which is then rounded using the ROUND function. Finally, the result is converted back to an interval using the CAST function.

The output of the above query would be:

original_interval | rounded_interval -------------------+----------------- 1 day 02:30:00 | 1 day 02:30:00

As you can see, the interval 1 day 2 hours 30 minutes is rounded to 1 day 02:30:00, which represents 1 day and 2 hours rounded to the nearest minute.