Skip to content Skip to sidebar Skip to footer

How To Split Data From One Column To Three Columns In Another Table? Sql

I have table (inf) with two columns id_student and hobbies like this: ID_student = 1 Hobbies = 'music, cooking, bassguitar' and I want to copy the hobbies by splitting t

Solution 1:

There is many ways to do this. One way is using the string_to_array function:

INSERT INTO hobbies (id, hobby1, hobby2, hobby3) 
SELECT id,hobbies_array[1],hobbies_array[2],hobbies_array[3] FROM 
  (
    SELECT id,string_to_array(hobbies,',') AS hobbies_array 
    FROM inf
  ) AS foo;

Post a Comment for "How To Split Data From One Column To Three Columns In Another Table? Sql"