开发者

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

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开发者_运维知识库?


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>


"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.


Yes, it's possible with one REPLACE() function. Try:

update nnn set c1 = REPLACE(c1,'ABC>','XYZ>')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜