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!

How to concatenate strings having apostrophe and ampersand

943026Jul 20 2014 — edited Jul 20 2014

Hi All,

I have a task to concatenate two columns below  SID - SNAME.

Input parameter is :sid. We can pass multiple parameters to :sid

CREATE TABLE TABLE1 ( SID NUMBER, SNAME  VARCHAR2(100));

Insert into TABLE1 (SID,SNAME) values (1,'SURF'S UP');

Insert into TABLE1 (SID,SNAME) values (2,'GNLV STYLE & KEY');

Insert into TABLE1 (SID,SNAME) values (3,'ESSENTIALS NORTH & SOUTH');

Insert into TABLE1 (SID,SNAME) values (4,'D'Ora');

select * from TABLE1;

SIDSNAME
1SURF'S UP
2GNLV STYLE & KEY
3ESSENTIALS NORTH & SOUTH
4D'Ora

Expected Output

1. with parameter input :sid in (1,2)

Output :    1 - SURF'S UP , 2 - GNLV STYLE & KEY

2. with parameter input : sid in (1,2,3,4)

Output:    1 - SURF'S UP , 2 - GNLV STYLE & KEY , 3 - ESSENTIALS NORTH & SOUTH , 4 - D'Ora

I tried using the below query,

select (select  RTRIM ( xmlagg (xmlelement (c, sid ||' - '||sname||',') ).extract ('//text()'), ',' )

AS concatenated FROM table1 where ',' || :para || ',' like '%,'|| sid || ',%') as concat  from dual ;

Output what i am getting is as below :

1 - SURF'S UP , 2 - GNLV STYLE & TREND

I am not handling apstrophe and ampersand correctly.

Can you please help me.

Thanks,

This post has been answered by odie_63 on Jul 20 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2014
Added on Jul 20 2014
4 comments
4,254 views