select text field separated by comma return lines

Asked

Viewed 1,548 times

1

Does anyone know how it is possible in postgresql take a value within a field of type TEXT which has comma-separated records to use in the WHERE?

Example of field values (35693,35694,35695,35696,35697,35698,35699,713)

Example

Select * from tabela where id = '35693'

Put the value on where is inside a string separated by comma, how to break and compare?

3 answers

1

You can use the function array_position combined with the function regexp_split_to_array.

array_position(regexp_split_to_array(seu_campo_texto), elemento_a_pesquisar) NOT NULL

If the function array_position returns NULL it is because the element does not exist in the text.

Another possibility, simpler, is to use the function position:

position(elemento in seu_campo_texto)

0

I managed to solve using :

UNNEST(ARRAY[string_to_array(campo_pesquizar, ',')])::INT

-1

SELECT *
FROM tabela
WHERE id IN (35693,35694,35695,35696,35697,35698,35699,713);

Sorry this suggestion returns values for all fields in the list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.