Home > front end >  Split data from a column and insert to another table in postgresql
Split data from a column and insert to another table in postgresql

Time:10-18

I'm trying to split data from Username column in a table like this:

Table1
ID  Username
1   UserA,UserB,UserC

and I want to insert it to another table. the result will be like this:

Table2
ID  Username
1   UserA
1   UserB
1   UserC

is this possible to do this in postgresql?

thanks in advance

CodePudding user response:

You can split the value and then unnest it:

insert into table2 (id, username)
select t1.id, ut.username
from table1 t1
  cross join unnest(string_to_array(t1.username), ',')) as ut(username)
  • Related