I have a table that looks like that
ID | Code |
---|---|
1 | Value1,Value2,Value3 |
What I need to do is to create an extra column for the "values" and copy them to the new rows What's the best approach to tackle the task?
Thank you!
CodePudding user response:
Create an array and unnest this array to create different records. Something like this:
SELECT unnest(string_to_array('Value1,Value2,Value3', ','));
CodePudding user response:
In Postgres 14 or later you can use string_to_table()
select t.id, x.code
from the_table t
cross join string_to_table(code, ',') as x(code)
For older version use the combination of string_to_array()
and unnest()
.