Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search if jsonb array of string contains value

I have arry of strings in a column like : ["196 2616", "9503744", "36.25260-6027", "2 414 425", "7 034 771 6", "F709714", "1088229", "183144", "505870338", "105075"]

I want to search if this array contains for example 2 414 425.

I tried something like this:
SELECT * FROM table_name t where t.numbers @> '2 414 425'
But it doesnt return anything even when it should.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The @> operator needs a proper JSON object on the right hand side:

where t.numbers @> '["2 414 425"]'

This should work if numbers is defined as jsonb (which it should be). Otherwise cast it numbers::jsonb

Checking if at least one of multiple keys is contained in the JSON array can be done using the ?| operator:

where t.numbers ?| array['2 414 425', '13261999']

The ?& operator will check if all those keys are contained in the JSON array.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading