Skip to content
Advertisement

SQL concatenate – INSERT MANY VALUES TO ONE ROW

I’m currently trying to add 3 rows in to 1 row so that the information is separated by a ‘,’like using a concatenation. However, I’m new to this and any help will be useful. Below is my current code and what I have tried.

This code below does not work:

insert into GENRES (GENRES) 
select VALUES GENRE1,GENRE2,GENRE3 
from TITLES;

I have also tried this code below, which inserted the rows but not in the format I want:

insert into GENRES (GENRES) 
SELECT GENRE1 FROM titles
union all
SELECT GENRE2 FROM titles
union all
SELECT GENRE3 FROM titles;

Which displayed:

  GENRES
1 ACTION
2 ADVENTURE
3 ROMANCE

I would need it to display in this format below for example:

  GENRES 
1 Action, Adventure, Romance

Advertisement

Answer

Concatenation it is.

insert into genres (genres)
select genre1 ||', '|| genre2 ||', '|| genre3
from titles
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement