Home > OS >  Find a range into an sql array
Find a range into an sql array

Time:01-10

How to find a matching range based on a given value x

For example, in column A, we have the value: {15-17,18-20,21-23}

The output expected for the x=16 should be 15-17 or x=21 should be 21-23

Any idea of the best way to achieve that ?

CodePudding user response:

Try

select rangetext 
from unnest('{15-17,18-20,21-23}'::text[]) as t(rangetext)
where 16 between split_part(rangetext , '-', 1)::integer 
             and split_part(rangetext , '-', 2)::integer;
  • Related