Oracle 23.26Data typesLab-verified · 2026-11-29

ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion

A CLOB or BLOB longer than the SQL VARCHAR2/RAW limit was converted implicitly or explicitly.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 5000, maximum: 4000)

Jump to a cause

  1. 01TO_CHAR on a CLOB longer than 4000 characters
  2. 02INSERT SELECT a CLOB column into a VARCHAR2(4000) column
  3. 03UTL_RAW.CAST_TO_VARCHAR2 on a BLOB over 2000 bytes

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-29 · container image gvenzl/oracle-free:23-slim

In SQL, a CLOB can be turned into VARCHAR2 only up to 4000 bytes, and a BLOB into RAW only up to 2000 bytes here. When the LOB is longer, Oracle stops with ORA-22835 and tells you the real size: (actual: 5000, maximum: 4000).

The fix is always the same idea: read the LOB in pieces with DBMS_LOB.SUBSTR, or keep it as a LOB. Check the length first with DBMS_LOB.GETLENGTH. All three cases below were run on Oracle 26ai Free.

01TO_CHAR on a CLOB longer than 4000 characters

The table has one CLOB of 5000 characters. SELECT TO_CHAR(body) fails with (actual: 5000, maximum: 4000).

DBMS_LOB.GETLENGTH(body) confirmed 5000. Reading it in pieces works: DBMS_LOB.SUBSTR(body, 4000, 1) and DBMS_LOB.SUBSTR(body, 4000, 4001) returned 4000 and 1000 characters.

Setup
Setuprun as lab_a
CREATE TABLE docs (id NUMBER PRIMARY KEY, body CLOB);
INSERT INTO docs VALUES (1, TO_CLOB(RPAD('A', 4000, 'A')) || RPAD('B', 1000, 'B'));
COMMIT;
OUTPUT
Table created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT TO_CHAR(body) AS body_text FROM docs;
✕ ERROR
SELECT TO_CHAR(body) AS body_text FROM docs
       *
ERROR at line 1:
ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 5000, maximum: 4000)
Help: https://docs.oracle.com/error-help/db/ora-22835/
Checkrun as lab_a
SELECT id, DBMS_LOB.GETLENGTH(body) AS len FROM docs;
OUTPUT
        ID        LEN
---------- ----------
         1       5000

1 row selected.

DBMS_LOB.SUBSTR(lob, amount, offset) takes the amount first, then the starting position.

After the fixrun as lab_a
SELECT LENGTH(DBMS_LOB.SUBSTR(body, 4000, 1)) AS first_part,
       LENGTH(DBMS_LOB.SUBSTR(body, 4000, 4001)) AS second_part
FROM docs;
✓ SUCCESS
FIRST_PART SECOND_PART
---------- -----------
      4000        1000

1 row selected.

02INSERT SELECT a CLOB column into a VARCHAR2(4000) column

Copying docs.body (CLOB) into docs_copy.body VARCHAR2(4000) with INSERT … SELECT fails with the same ORA-22835. Oracle converts the CLOB implicitly and the 5000-character value does not fit.

Cutting it to 4000 with DBMS_LOB.SUBSTR(body, 4000, 1) inserted the row, and the copy has length 4000. The last 1000 characters are lost, so if you need the full text, make the target column a CLOB instead.

Setup
Setuprun as lab_a
CREATE TABLE docs (id NUMBER PRIMARY KEY, body CLOB);
INSERT INTO docs VALUES (1, TO_CLOB(RPAD('A', 4000, 'A')) || RPAD('B', 1000, 'B'));
COMMIT;
CREATE TABLE docs_copy (id NUMBER PRIMARY KEY, body VARCHAR2(4000));
OUTPUT
Table created.


1 row created.


Commit complete.


Table created.
Reproducerun as lab_a
INSERT INTO docs_copy SELECT id, body FROM docs;
✕ ERROR
INSERT INTO docs_copy SELECT id, body FROM docs
                                 *
ERROR at line 1:
ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 5000, maximum: 4000)
Help: https://docs.oracle.com/error-help/db/ora-22835/
Checkrun as lab_a
SELECT id, DBMS_LOB.GETLENGTH(body) AS len FROM docs;
OUTPUT
        ID        LEN
---------- ----------
         1       5000

1 row selected.
After the fixrun as lab_a
INSERT INTO docs_copy SELECT id, DBMS_LOB.SUBSTR(body, 4000, 1) FROM docs;
SELECT id, LENGTH(body) AS len FROM docs_copy;
✓ SUCCESS
1 row created.


        ID        LEN
---------- ----------
         1       4000

1 row selected.

03UTL_RAW.CAST_TO_VARCHAR2 on a BLOB over 2000 bytes

Reading a 3000-byte BLOB as text with UTL_RAW.CAST_TO_VARCHAR2(data) fails with (actual: 3000, maximum: 2000). The BLOB is first converted to RAW, and in this SQL the limit was 2000 bytes.

DBMS_LOB.GETLENGTH(data) showed 3000 bytes. Taking the first 2000 bytes with DBMS_LOB.SUBSTR(data, 2000, 1) before the cast worked and returned 2000 characters.

Setup
Setuprun as lab_a
CREATE TABLE files (id NUMBER PRIMARY KEY, data BLOB);
DECLARE
  b BLOB;
BEGIN
  INSERT INTO files VALUES (1, EMPTY_BLOB()) RETURNING data INTO b;
  DBMS_LOB.WRITEAPPEND(b, 2000, UTL_RAW.CAST_TO_RAW(RPAD('X', 2000, 'X')));
  DBMS_LOB.WRITEAPPEND(b, 1000, UTL_RAW.CAST_TO_RAW(RPAD('Y', 1000, 'Y')));
  COMMIT;
END;
/
OUTPUT
Table created.


PL/SQL procedure successfully completed.
Reproducerun as lab_a
SELECT UTL_RAW.CAST_TO_VARCHAR2(data) AS txt FROM files;
✕ ERROR
SELECT UTL_RAW.CAST_TO_VARCHAR2(data) AS txt FROM files
                                *
ERROR at line 1:
ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 3000, maximum: 2000)
Help: https://docs.oracle.com/error-help/db/ora-22835/
Checkrun as lab_a
SELECT id, DBMS_LOB.GETLENGTH(data) AS bytes FROM files;
OUTPUT
        ID      BYTES
---------- ----------
         1       3000

1 row selected.
After the fixrun as lab_a
SELECT LENGTH(UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(data, 2000, 1))) AS first_part FROM files;
✓ SUCCESS
FIRST_PART
----------
      2000

1 row selected.

FAQ

How do I select a CLOB longer than 4000 characters?

Read it in 4000-character pieces with DBMS_LOB.SUBSTR(col, 4000, 1), DBMS_LOB.SUBSTR(col, 4000, 4001) and so on, or fetch it as a CLOB in your application without TO_CHAR.

What do "actual" and "maximum" mean in ORA-22835?

actual is the LOB length, maximum is the limit of the conversion. On this page they were 5000/4000 for CLOB to VARCHAR2 and 3000/2000 for BLOB to RAW.

Why does INSERT SELECT from a CLOB into VARCHAR2 fail?

Oracle converts the CLOB to VARCHAR2 implicitly. If any row is longer than the column allows, you get ORA-22835. Cut it with DBMS_LOB.SUBSTR, or make the target column a CLOB.

How do I check the size of a CLOB or BLOB?

DBMS_LOB.GETLENGTH(col) returns characters for a CLOB and bytes for a BLOB.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0