Date and Time calculations using INTERVAL – with examples in PostgreSQL.

Saying time is important is to state the obvious. Our lives are built on time. We only have so much of it and once it is gone, there is no getting it back. While not as important as that time, date and time data values are integral pieces in most –if not all– database infrastructure and design. At some point or another, date and time values must be considered. PostgreSQL has many functions and options for date and time value manipulation. In this post, I look at the INTERVAL type for date and time calculations….

bedside-clock
Photo by petradr on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:
  • Xubuntu Linux 18.04.2 LTS (Bionic Beaver)
  • PostgreSQL 11.2


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


I’ll use a couple of tables from the PostgreSQL practice DVD Rental database for the example queries below.

Let’s visit the arbitrary query below for customers whose first names begin with ‘Z’, limiting the results to just 10 for better on-screen readability:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
SELECT
        c.first_name AS first_name,
        c.last_name AS last_name,
        r.rental_date
FROM customer AS c
INNER JOIN rental AS r
ON c.customer_id = r.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 first_name | last_name |     rental_date    
------------+-----------+---------------------
 Zachary    | Hite      | 2005-05-25 21:46:54
 Zachary    | Hite      | 2005-05-29 03:48:01
 Zachary    | Hite      | 2005-06-17 04:35:52
 Zachary    | Hite      | 2005-06-17 08:02:20
 Zachary    | Hite      | 2005-06-17 10:40:36
 Zachary    | Hite      | 2005-06-17 16:33:17
 Zachary    | Hite      | 2005-06-18 14:55:30
 Zachary    | Hite      | 2005-06-19 17:27:25
 Zachary    | Hite      | 2005-07-06 01:36:53
 Zachary    | Hite      | 2005-07-06 14:57:53
(10 rows)

Suppose you need to know when 7 days have elapsed for a rented DVD. You can use INTERVAL and perform the date arithmetic:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
SELECT
        c.first_name AS first_name,
        c.last_name AS last_name,
        r.rental_date AS rent_day,
        r.rental_date + INTERVAL '7 DAY' AS late_return_day
FROM customer AS c
INNER JOIN rental AS r
ON c.customer_id = r.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 first_name | last_name |      rent_day       |   late_return_day  
------------+-----------+---------------------+---------------------
 Zachary    | Hite      | 2005-05-25 21:46:54 | 2005-06-01 21:46:54
 Zachary    | Hite      | 2005-05-29 03:48:01 | 2005-06-05 03:48:01
 Zachary    | Hite      | 2005-06-17 04:35:52 | 2005-06-24 04:35:52
 Zachary    | Hite      | 2005-06-17 08:02:20 | 2005-06-24 08:02:20
 Zachary    | Hite      | 2005-06-17 10:40:36 | 2005-06-24 10:40:36
 Zachary    | Hite      | 2005-06-17 16:33:17 | 2005-06-24 16:33:17
 Zachary    | Hite      | 2005-06-18 14:55:30 | 2005-06-25 14:55:30
 Zachary    | Hite      | 2005-06-19 17:27:25 | 2005-06-26 17:27:25
 Zachary    | Hite      | 2005-07-06 01:36:53 | 2005-07-13 01:36:53
 Zachary    | Hite      | 2005-07-06 14:57:53 | 2005-07-13 14:57:53
(10 rows)

Be mindful of the surrounding quotes for the INTERVAL value, for without them, you get an error:

1
2
3
4
5
6
7
8
9
10
11
12
SELECT
        c.first_name AS first_name,
        c.last_name AS last_name,
        r.rental_date AS rent_day,
        r.rental_date + INTERVAL 7 DAY AS late_return_day
FROM customer AS c
INNER JOIN rental AS r
ON c.customer_id = r.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
ERROR:  syntax error at or near "7"
LINE 5:         r.rental_date + INTERVAL 7 DAY AS late_return_day

INTERVAL calculations can use hour time values as well.

Imagine the same DVD rental store has a ’12 Hours of Madness’ special where you can get any number of DVD’s you want to rent for $1/each, but they must be returned within 12 hours.

Use INTERVAL in the query to determine when the 12 hour time period is up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
dvdrental=> SELECT
        c.first_name AS first_name,
        c.last_name AS last_name,
        r.rental_date,
        r.rental_date + INTERVAL '12 HOUR' AS madness_hour
FROM customer AS c
INNER JOIN rental AS r
ON c.customer_id = r.customer_id
WHERE c.first_name LIKE 'Z%'
LIMIT 10;
 first_name | last_name |     rental_date     |    madness_hour    
------------+-----------+---------------------+---------------------
 Zachary    | Hite      | 2005-05-25 21:46:54 | 2005-05-26 09:46:54
 Zachary    | Hite      | 2005-05-29 03:48:01 | 2005-05-29 15:48:01
 Zachary    | Hite      | 2005-06-17 04:35:52 | 2005-06-17 16:35:52
 Zachary    | Hite      | 2005-06-17 08:02:20 | 2005-06-17 20:02:20
 Zachary    | Hite      | 2005-06-17 10:40:36 | 2005-06-17 22:40:36
 Zachary    | Hite      | 2005-06-17 16:33:17 | 2005-06-18 04:33:17
 Zachary    | Hite      | 2005-06-18 14:55:30 | 2005-06-19 02:55:30
 Zachary    | Hite      | 2005-06-19 17:27:25 | 2005-06-20 05:27:25
 Zachary    | Hite      | 2005-07-06 01:36:53 | 2005-07-06 13:36:53
 Zachary    | Hite      | 2005-07-06 14:57:53 | 2005-07-07 02:57:53
(10 rows)

Although this blog post touches on just a couple of simple example date and time calculations, check out INTERVAL for yourself to test all it has to offer..

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official PostgreSQL 11 On-line Documentation for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the β€˜Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

Hey thanks for commenting! Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.