Element of an array in a daterange?
I've got an array of dates, and I want to find out if any of the elements in the array appear within a given daterange.
Depending on the query, you can use
- row aggregation with
bool_or
mentioned by joanolo in his answer. or, you can use
ANY
with the contains operator@>
SELECT daterange('2017-01-01', '2017-01-31', '[]') @> ANY(ARRAY['2017-01-01'::date, '2017-02-03'::date]); SELECT daterange('2017-01-01', '2017-01-31', '[]') @> ANY(ARRAY['2016-01-02'::date, '2016-01-09'::date]);
You can use the aggregate function bool_or
to achieve what you're looking for:
SELECT
bool_or(date_range @> a_date) AS some_of_the_dates_lies_within_range
FROM
(
SELECT
unnest(ARRAY['2017-01-01'::DATE, '2017-02-03'::DATE]) AS a_date,
daterange('2017-01-01', '2017-01-31', '[]') AS date_range
) AS s0