franco_b
franco_b

Reputation: 878

Postgres Convert Text array to Int array

In order to use Unnest function I want convert a list to array.

This is my list of type text. It's an output of this function (How get all positions in a field in PostgreSQL?):

108,109,110,114,115,116,117,156,157,200,201,205

I convert to array with

array[108,109,110,114,115,116,117,156,157,200,201,205]

result is type text[]:

"{"108,109,110,114,115,116,117,156,157,200,201,205"}"

With this kind of array unnest function doesn't work so I think I want convert to array of Int

Thanks

Upvotes: 2

Views: 12604

Answers (2)

klin
klin

Reputation: 121504

with the_data(str) as (
    select '108,109,110,114,115,116,117,156,157,200,201,205'::text
)

select elem
from the_data,
unnest(string_to_array(str, ',')) elem;

 elem 
------
 108
 109
 110
 114
 115
 116
 117
 156
 157
 200
 201
 205
(12 rows)

Upvotes: 2

Oto Shavadze
Oto Shavadze

Reputation: 42753

If I correctly understand, you need this (no necessary convert to INT):

select unnest( string_to_array('108,109,110,114,115,116,117,156,157,200,201,205', ',' ) )

Upvotes: 1

Related Questions