Search code examples
postgresqlpostgresql-9.1sql-in

Postgresql parse string values to integers


Is there a way in Postgres I can parse string values to integers? Basically I'm trying to query one table (let's call it table_one) using values from another table (table_two) in a character varying column.

Say SELECT char_column FROM table_two results in "2,4,6,8", I'd like to use this result in a second query as;

SELECT column FROM table_one WHERE some_id IN (2,4,6,8)

How can I get the string "2,4,6,8" to values 2,4,6,8 so as to be able to use it in the second query?

I've tried casting and to_number functions to no success.


Solution

  • SELECT column
    FROM table
    WHERE other_column = ANY(string_to_array('2,4,6,8', ',')::INT[])