How do you query future date ranges to look for valid DATERANGE?


I’ve made a previous post over here going through date ranges in that is current and in the future.

However, I’ve not specified enough to include dates that would only happen in the future. I’m making a new post since there are no current post addressing this. Considering date ranges are a "weird" data type I figured it’d be best to address this in a separate post.

Say my table is this

validity table
 id          |     validity_dates      
          1 | [2018-01-01,2019-01-01)
          2 | [2017-05-01,2017-06-01)
          3 | [2016-05-01,2016-07-01)
          4 | [2022-01-01,2025-01-01)
          5 | [2024-01-01,2026-12-10)

How do I query to get the id based on whether the date range between validity_dates exists in the future?

For example I want to get id’s that are valid in future. I run a query and get a return on id 5 since the validity dates starts in 2024.

My current implementation doesn’t work in the sense it provides only dates that contain the current date into the date ranges and returns id 4

FROM validity
WHERE current_date <@ validity_dates

How do I best approach this?

>Solution :

Use lower():

FROM validity
WHERE current_date < LOWER(validity_dates);

Leave a ReplyCancel reply