Forum Stats

  • 3,837,059 Users
  • 2,262,222 Discussions
  • 7,900,192 Comments

Discussions

REPLACE not working - ASCII character - XML element

Jeevan Anand
Jeevan Anand Member Posts: 52 Red Ribbon

Hi,

I would like to persist the special character as it as in table column - CLOB.


When i execute below query, only & is replaced with & value, Other values are not replacing with actual special character.


Can you please assist.

Query:

SELECT      

REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(

RTRIM(XMLAGG(XMLELEMENT(E,'<>''"&',' \n ','Test&').EXTRACT('//text()')).GETCLOBVAL(),' \n '),

CHR(60)||'lt;',CHR(60)),

CHR(39)||'apos;',CHR(39)),

CHR(38)||'amp;',CHR(38)),

CHR(62)||'gt;',CHR(62)),

CHR(34)||'quot;',CHR(34)) as repl from dual;


Query Output:

&lt;&gt;&apos;&quot;& \n Test&


Expected Output:

<>''"& \n Test&

Tagged:

Best Answer

  • odie_63
    odie_63 Member Posts: 8,493 Silver Trophy
    Answer ✓

    I suggest removing all those REPLACEs and using dedicated function to cast XML nodes as scalar value :

    SELECT xmlcast(
             XMLAGG(XMLELEMENT(E,'<>''"&',' \n ','Test&'))
             as clob
           ) as result
    from dual
    ;
     
    RESULT
    --------------------------------------------------------------------------------
    <>'"& \n Test&
     
    


    Frank KulashJeevan Anand

Answers

  • BEDE
    BEDE Oracle Developer Member Posts: 2,453 Gold Trophy

    You are executing that in some SQL window in sqlplus or sqldeveloper or some other. Is it so?

    Then, the solution is to run: set define off before executing that SQL statement containing &.

  • Jeevan Anand
    Jeevan Anand Member Posts: 52 Red Ribbon

    @BEDE This query is used inside stored procedure to insert into table.

    For ex: insert into <table_name> (clob column) select <statement>.

  • odie_63
    odie_63 Member Posts: 8,493 Silver Trophy
    Answer ✓

    I suggest removing all those REPLACEs and using dedicated function to cast XML nodes as scalar value :

    SELECT xmlcast(
             XMLAGG(XMLELEMENT(E,'<>''"&',' \n ','Test&'))
             as clob
           ) as result
    from dual
    ;
     
    RESULT
    --------------------------------------------------------------------------------
    <>'"& \n Test&
     
    


    Frank KulashJeevan Anand