SELECT as much data as possible from CLOB to VARCHAR2, with multi-byte characters in the data

Multibyte characters caused me great pain.

Any suggestion on this issue?

I have a CLOB field that can contain several multi-byte characters, and I need to select in SQL and convert this field to a string for the next process, currently I use:

SELECT DBMS_LOB.SUBSTR( description, 4000, 1 ) FROM table 

But the 4000 command in the above expression has a character length, not a byte. Therefore, I had to change to 3000 to handle any multibyte characters that could be painted over in the data, and a buffer size error would occur.

The problem is that records that do not contain a multibyte character can unnecessarily truncate more data than necessary. (4000 is a row limit, we can / should live with it.)

Is there a way to do something in the equivalent:

 SELECT DBMS_LOB.SUBSTR( description, 4000bytes, 1 ) FROM table 

This way I can get as much data as possible.

Note. I am not allowed to create temporary tables / views without using PL / SQL, only SQL SELECT ...

+7
source share
2 answers

Jeffrey's thinking process is fine, but alchn is also right. Just ran into this problem, and here is my solution. You will need to be able to create a function:

 Create Or Replace Function clob_substr(p_clob In Clob ,p_offset In Pls_Integer ,p_length In Pls_Integer) Return Varchar2 Is Begin Return substrb(dbms_lob.substr(p_clob ,p_length ,p_offset) ,1 ,p_length); End; / 

Here is a demonstration of use:

 Select c ,clob_substr(c ,1 ,4000) From ( Select xmlelement("t", rpad('é', 4000, 'é'), rpad('é', 4000, 'é')).extract('//text()').getclobval() c From dual ); 
+7
source

Perhaps we trim the resulting varchar2 with SUBSTR:

 SELECT SUBSTRB( DBMS_LOB.SUBSTR( description, 4000, 1 ), 1, 4000) FROM table 
+4
source

All Articles