I experimented a little bit with clobs and varchar2. The thing is that we have a lot of XML in a database table and I wanted to download the XML as fast as possible. As I need to use the C# DevArt components I tried it various ways and found out that clobs (they involve one roundtrip to the database per row) are a lot slower than varchar2.
Thus I am right now downloading the data in two steps. First as much as I can as varchar2 (4000 Byte limit) and then everything else as clob.
Now the tricky part. I came up with the following query to find out what I can retrieve as varchar2:
select c.xml.getstringval() from customers c where
length(t.xml.getclobval()) = length(dbms_lob.substr(c.xml.getclobval(), 4000, 1))
The query is based on the assumption that length is returning the number of characters for clobs and substr is returning the a string with a certain number of bytes. It doesn't work only with length because then I run into trouble with multibytecharacters like à é oder è. But now I have another very strange behaviour. The query above works for most of the rows but for some of them it still throws an ORA-06502. But if I change the limit to 3992 it always works??? (as information, we also store a thumbnail of a picture as Base64 encoded string in the xml).
Does anyone now why the query doesn't work in all cases. Or does anyone have a suggestion how to write a query that find out if a clob can be retrieved as a varchar2?