Shalahuddin Al-Ayyubbi
Shalahuddin Al-Ayyubbi

Reputation: 408

How to convert one row text from a result of a query into a string that can be used in another query in PostgreSQL?

I have a query like this:

select value from some_table_name where key='some_key'

The result is 1 row only. It is a text with commas

apple,banana,orange

How can I use that result in this below query?

SELECT unnest(
  string_to_array('use above text from query in here', ',')
) AS parts;

Upvotes: 0

Views: 25

Answers (1)

user330315
user330315

Reputation:

You can use the unnest directly in the FROM clause:

select u.* 
from some_table_name t
  cross join unnest(string_to_array(t.value, ',')) as u(val)
where t.key='some_key';

Or with a current version of Postgres:

select u.* 
from some_table_name t
  cross join string_to_table(t.value, ',') as u(val)
where t.key='some_key'

Upvotes: 1

Related Questions