How to calculate size of a CLOB in Bytes?

Recently i was dealing with a translation problem regarding multibyte characters (cyrillic). They were stored in a NCLOB and were later on moved to a VARCHAR2.

Before moving the data i had to ensure, that there will be enough space in the VARCHAR2 columns which were defined as VARCHAR2(255 BYTE).

That’s where my travel started, i had to figure out how many bytes a CLOB-Value would consume when storing in a VARCHAR2 column.

LENGTH(<clob-column>) fails, because it returns the number of characters

DBMS_LOB.getLength(<clob-column>) fails as well, because it returns number of characters

LENGTHB doesn’t work on CLOBs, so i thought: implicit type conversion, so i came up with this

LENGTHB(DBMS_LOB.SUBSTR(<clob-column>,1,2000)) this one looked promising, but it failed at the end because it would return the number of bytes this string uses in the CLOB Characterset (which is AL16UTF16)

The winner is: LENGTHB(TO_CHAR(SUBSTR(<clob-column>,1,4000))) this one really returns the size in bytes the string would consume when stored in a VARCHAR2 column.

Thanks to explicit type conversion!



  1. It‘s quite in here! Why not leave a response?