Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Concatenate on Insert query help

newbiegalMay 18 2017 — edited May 22 2017

Hi Friends,

I need help with an insert statement below to concatenate LONG fields..

Source Table:

CREATE TABLE TS_TAB

(

  ID_TGRP              VARCHAR2(255 CHAR),

  TAB_NUMBER           VARCHAR2(255 CHAR),

  J_PERSON             NUMBER(22),

  J_TEXT               LONG,

  J_SEQ                NUMBER(22)

);

Sample data:

Insert into ts_tab values ('1002','1002',130,'12/10/02 Express',1);

Insert into ts_tab values ('1002','1002',130,'MLS',2);

Insert into ts_tab values ('1002','1002',130,'BY AIRWAY',3);

Insert into ts_tab values ('1002','1002',130,'7/5/01 #4 HOLD',4);

Insert into ts_tab values ('1002','1002',130,'7/5/01 #9 HOLD',5);

Insert into ts_tab values ('1002','1002',130,'7/5/01 #11 HOLD',6);

Insert into ts_tab values ('J256','J256',187,'10/10/01 Express',7);

Insert into ts_tab values ('J256','J256',187,'MLS',8);

Insert into ts_tab values ('J256','J256',187,'BY ROAD',9);

Insert into ts_tab values ('J256','J256',187,'10/7/01 #1 HOLD',10);

Insert into ts_tab values ('KL61','KL61',211,'SHIP WRNG',11);

Insert into ts_tab values ('KL61','KL61',211,'ASKD FOR COPY',12);

Target table:

SQL> DESC TS_COMM

Name                                      Null?    Type

----------------------------------------- -------- ---------------------

C_PERSON                                  NOT NULL NUMBER(22)

G_COMP                                    NOT NULL VARCHAR2(10 CHAR)

C_COMMENT                                          LONG

TYPE                                               VARCHAR2(20 CHAR)

COMM_NEXT                                 NOT NULL NUMBER(22)

Criteria to populate the target table:

Insert into ts_comm

(C_PERSON,G_COMP,C_COMMENT,TYPE,COMM_NEXT)

select

ts.j_person     C_PERSON

,'I'                   G_COMP

, -- Concatenate ts_tab.j_text for the same ts_tab.id_tgrp  with a line break          C_COMMENT

,'STAT'           TYPE

,(select s.next_num from short_screen s where screen_no =10)+rownum          COMM_NEXT

from ts_tab ts;

Result should look like:

1st result record in TS_COMM

 

c_person 130
g_compI
c_comment12/10/02 Express
MLS
BY AIRWAY
7/5/01 #4 HOLD
7/5/01 #9 HOLD
7/5/01 #11 HOLD
typeSTAT
comm_next300

2nd result record in TS_COMM  

 

c_person 130
g_compI
c_comment12/10/02 Express
MLS
BY AIRWAY
7/5/01 #4 HOLD
7/5/01 #9 HOLD
7/5/01 #11 HOLD
typeSTAT
comm_next300

I'm not sure how to concatenate the LONG fields for all the same id_tgrp and also with a line break.. Please help..Thanks in advance..

This post has been answered by jaramill on May 18 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 19 2017
Added on May 18 2017
12 comments
1,159 views