Is it possible to update data inside a CLOB using SQL?

55,189

Solution 1

"i have new line in the column. any advice?"

Newlines are characters; if you want to amend text which contains them you need to include them in the search string. You can do this using the CHR() which takes an ASCII value as an argument. The precise codes you need to include vary according to OS. Because I ran this example on MS Windows I needed to pass both linefeed (ASCII=10) and carriage return (ASCII=13).

SQL> select * from t42
  2  /

TXT
--------------------------------------------------------------------------------
<ABC> ABCD
  </ABC>


SQL>  update t42 set txt=replace(txt,'ABCD'||chr(10)||chr(13), 'APC woz here')
  2  /

1 row updated.

SQL> select * from t42
  2  /

TXT
--------------------------------------------------------------------------------
<ABC> APC woz here </ABC>

SQL>

Incidentally, if you are storing XML text it might be worthwhile using the XMLType datatype for the column instead of CLOB. It comes with a lot of useful functionality.

Solution 2

Why not try it ?

SQL> create table nnn(c1 clob);

Table created.

SQL> insert into nnn values ('text ABC end');

1 row created.

SQL> select * from nnn;

C1
-------------------------------------------------
text ABC end

SQL> update nnn set c1=replace(c1,'ABC','XYZ');

1 row updated.

SQL> select * from nnn;

C1
-------------------------------------------------
text XYZ end

SQL>
Share:
55,189
Hemant
Author by

Hemant

Updated on April 20, 2020

Comments

  • Hemant
    Hemant about 4 years

    I have a table having one clob column which has XML data in it. Say I want to replace XYZ with ABC in the clob column. Is it possible using sqlplus?