user2771151
user2771151

Reputation: 421

split columns by a delimiter in postgres

I have a large table key(keyid,data) . In this table data consists of a text separated by /. Eg x/y/z . I wish to extract the 2nd field (in the example y) for all the values stored in datails column in the table.

I tried using these

dblp1=# select regexp_split_to_array((select key from keytable),'/') as key_split;

ERROR: more than one row returned by a subquery used as an expression

dblp1=# SELECT split_part((select key from keytable), '/', 2);     

ERROR: more than one row returned by a subquery used as an expression

Both work on single string .

Upvotes: 2

Views: 4911

Answers (1)

Wayne Conrad
Wayne Conrad

Reputation: 107999

Pretty close. You need the function to be wrapped right around the column name, like so:

select split_part(key, '/', 2) from keytable;  

Upvotes: 5

Related Questions