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

Postgresql: find multiple records from comma separated column

I am using PostgreSQL with Laravel 8.

In a Postgres table, I have one comma separated character varying column

enter image description here

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

I need those records from this table where value of this comma separated column is 43 OR 56 OR 252.

>Solution :

Storing comma separated values in a single column is a huge mistake to begin with. But if you can’t change the data model, you can achieve what you want by converting the value into an array, then use the array overlaps operator && with the values you want to test for:

select *
from the_table
where string_to_array(bad_column, ',')::int[] && array[43, 56, 252];
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