Home > database >  Postgres string to array and check if includes value
Postgres string to array and check if includes value

Time:12-18

I have got a table employer_entity with column tokens. This column is a varchar and it contains many tokens split by comma, for instance:

id | tokens
---|------------------
 1 | aaaaa,bbbbb,ccccc

Now I want to build a query that will check if tokens column contains the token aaaaa. Here is what I've got

select *
from employer_entity
where 'aaaaa'
in (string_to_array(employer_entity.tokens, ','))

But it throws an error

Array value must start with "{" or dimension information.

CodePudding user response:

You're looking for the any operator, not the in operator:

SELECT *
FROM   employer_entity
WHERE  'aaaaa' = ANY(STRING_TO_ARRAY(employer_entity.tokens, ','))
  • Related