Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Code Sample: Replace String (varchar) text in a CLOB
Spent alot of time trying to find some sample code to replace all
occurrences of some text with new text for CLOB variables in PLSQL.
Didnt find any, but noticed a few other posts looking for similiar
code... so i broke down and wrote one. Here it is for anyone
interested:
FUNCTION replaceClob
( srcClob IN CLOB, replaceStr IN varchar2, replaceWith IN varchar2 )
l_buffer VARCHAR2 (32767); l_amount BINARY_INTEGER := 32767; l_pos INTEGER := 1; l_clob_len INTEGER; newClob clob := EMPTY_CLOB;
BEGIN
l_clob_len := DBMS_LOB.getlength (srcClob); WHILE l_pos < l_clob_len LOOP DBMS_LOB.READ (srcClob,l_amount,l_pos,l_buffer); IF l_buffer IS NOT NULL THEN -- replace the text l_buffer := replace(l_buffer,replaceStr,replaceWith); -- write it to the new clob DBMS_LOB.writeAppend(newClob, LENGTH(l_buffer), l_buffer); END IF; l_pos := l_pos + l_amount; END LOOP; RETURN newClob;
EXCEPTION
WHEN OTHERS THEN
RAISE;
END;
![]() |
![]() |