Search code examples
sqlpostgresqldatetimeintervals

Can I search if an interval is between another interval?


I need to search if a date, or any day 30 days from that date is between an interval. Is that achievable somehow using postgresql?


Solution

  • You could simply check for interval overlaps:

    SELECT daterange(current_date, current_date + 31) && '[2022-01-01,2022-01-31]';
    

    There is also tsrange for ranges of timestamp and tstzrange for ranges of timestamp with time zone.