Arsalan Sherwani
Arsalan Sherwani

Reputation: 560

Concat Results of 2 Select Queries into 1 Column (oracle)

Im trying to insert a record into my table. But there is 1 column in which I want to get concatenated results of 2 select statements. Like the 2 statements will fetch their records and concatenate to form 1 value so that it can be inserted into the column.

insert into ABC (Name,City,Age) 
 Values ('John',(
           (Select City from TableA where ID=1)concat(Select City from TableA where ID=2)),'22')

Or it can be comma separated but I am not getting what to use here.

Upvotes: 0

Views: 135

Answers (3)

APC
APC

Reputation: 146349

Using a cross join to select from the two tables produces a nice clear statement:

insert into ABC (Name,City,Age) 
select 'John', concat(t1.city, t2.city), 22
from TableA t1
     cross join TableA t2
where t1.ID = 1
and t2.ID = 2
/

Upvotes: 1

mehmet sahin
mehmet sahin

Reputation: 812

Try this one:

    INSERT INTO ABC (Name, City, Age)
         VALUES ('John',
                 ( 
                   (SELECT City FROM TableA WHERE ID = 1) || 
                   (SELECT City FROM TableA WHERE ID = 2)
                 ),
                 '22');

But ensure ... WHERE ID = 1 and ....WHERE ID = 2 return one row.

Upvotes: 1

dmkov
dmkov

Reputation: 334

Use CONCAT() or CONCAT_WS() functions for this (reference)

insert into ABC (Name,City,Age) Values (
  'John', 
  CONCAT_WS(' ', (Select City from TableA where ID=1), (Select City from TableA where ID=2)),
  '22'
 )

Upvotes: 0

Related Questions