Oracle 23.26SQLLab-verified · 2026-11-29

ORA-01489: result of string concatenation is too long

A LISTAGG or || result became longer than the VARCHAR2 limit of the SQL engine.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-01489: result of string concatenation is too long

Jump to a cause

  1. 01LISTAGG result over 4000 bytes
  2. 02LISTAGG over 4000 bytes when you need the full list
  3. 03VARCHAR2 || VARCHAR2 longer than 4000

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, LISTAGG and the || operator return VARCHAR2. When the result goes past the limit (4000 bytes by default, MAX_STRING_SIZE=STANDARD), Oracle raises ORA-01489.

The usual case is LISTAGG over many rows. Below are three runs on Oracle 26ai Free: cut the list with ON OVERFLOW TRUNCATE, build a CLOB with XMLAGG, or make the || result a CLOB with TO_CLOB.

01LISTAGG result over 4000 bytes

500 codes of 11 characters plus a comma add up to 6000 bytes. LISTAGG(code, ',') WITHIN GROUP (ORDER BY id) failed with ORA-01489.

With LISTAGG(code, ',' ON OVERFLOW TRUNCATE '...' WITH COUNT) the query ran: the result was 3968 characters and ended with ITEM-000330,...(170), meaning 170 values were left out. Use this when a shortened list is acceptable.

Setup
Setuprun as lab_a
CREATE TABLE items (id NUMBER PRIMARY KEY, code VARCHAR2(20));
INSERT INTO items SELECT level, 'ITEM-' || LPAD(level, 6, '0') FROM dual CONNECT BY level <= 500;
COMMIT;
OUTPUT
Table created.


500 rows created.


Commit complete.
Reproducerun as lab_a
SELECT LISTAGG(code, ',') WITHIN GROUP (ORDER BY id) AS codes FROM items;
✕ ERROR
SELECT LISTAGG(code, ',') WITHIN GROUP (ORDER BY id) AS codes FROM items
                                                                   *
ERROR at line 1:
ORA-01489: result of string concatenation is too long
Help: https://docs.oracle.com/error-help/db/ora-01489/
Checkrun as lab_a
SELECT COUNT(*) AS cnt, SUM(LENGTHB(code) + 1) AS total_bytes FROM items;
OUTPUT
       CNT TOTAL_BYTES
---------- -----------
       500        6000

1 row selected.
After the fixrun as lab_a
COLUMN tail FORMAT A20
SELECT LENGTH(LISTAGG(code, ',' ON OVERFLOW TRUNCATE '...' WITH COUNT) WITHIN GROUP (ORDER BY id)) AS len,
       SUBSTR(LISTAGG(code, ',' ON OVERFLOW TRUNCATE '...' WITH COUNT) WITHIN GROUP (ORDER BY id), -20) AS tail
FROM items;
✓ SUCCESS
       LEN TAIL
---------- --------------------
      3968 ITEM-000330,...(170)

1 row selected.

02LISTAGG over 4000 bytes when you need the full list

Same 500 rows, same ORA-01489 from LISTAGG. If every value is needed, aggregate into a CLOB instead.

XMLCAST(XMLAGG(XMLELEMENT(e, code || ',') ORDER BY id) AS CLOB) returned a CLOB of 6000 characters, the full list.

Setup
Setuprun as lab_a
CREATE TABLE items (id NUMBER PRIMARY KEY, code VARCHAR2(20));
INSERT INTO items SELECT level, 'ITEM-' || LPAD(level, 6, '0') FROM dual CONNECT BY level <= 500;
COMMIT;
OUTPUT
Table created.


500 rows created.


Commit complete.
Reproducerun as lab_a
SELECT LISTAGG(code, ',') WITHIN GROUP (ORDER BY id) AS codes FROM items;
✕ ERROR
SELECT LISTAGG(code, ',') WITHIN GROUP (ORDER BY id) AS codes FROM items
                                                                   *
ERROR at line 1:
ORA-01489: result of string concatenation is too long
Help: https://docs.oracle.com/error-help/db/ora-01489/
Checkrun as lab_a
SELECT COUNT(*) AS cnt FROM items;
OUTPUT
       CNT
----------
       500

1 row selected.
After the fixrun as lab_a
SELECT DBMS_LOB.GETLENGTH(XMLCAST(XMLAGG(XMLELEMENT(e, code || ',') ORDER BY id) AS CLOB)) AS clob_len FROM items;
✓ SUCCESS
  CLOB_LEN
----------
      6000

1 row selected.

03VARCHAR2 || VARCHAR2 longer than 4000

body held 3000 characters. body || RPAD('B', 2000, 'B') would be 5000 and failed with ORA-01489.

Converting the first operand with TO_CLOB(body) || RPAD('B', 2000, 'B') made the result a CLOB, and LENGTH returned 5000.

Setup
Setuprun as lab_a
CREATE TABLE notes (id NUMBER PRIMARY KEY, body VARCHAR2(4000));
INSERT INTO notes VALUES (1, RPAD('A', 3000, 'A'));
COMMIT;
OUTPUT
Table created.


1 row created.


Commit complete.
Reproducerun as lab_a
SELECT LENGTH(body || RPAD('B', 2000, 'B')) AS len FROM notes;
✕ ERROR
SELECT LENGTH(body || RPAD('B', 2000, 'B')) AS len FROM notes
                                                        *
ERROR at line 1:
ORA-01489: result of string concatenation is too long
Help: https://docs.oracle.com/error-help/db/ora-01489/
Checkrun as lab_a
SELECT LENGTH(body) AS len FROM notes;
OUTPUT
       LEN
----------
      3000

1 row selected.
After the fixrun as lab_a
SELECT LENGTH(TO_CLOB(body) || RPAD('B', 2000, 'B')) AS len FROM notes;
✓ SUCCESS
       LEN
----------
      5000

1 row selected.

FAQ

How do I avoid ORA-01489 with LISTAGG?

Add ON OVERFLOW TRUNCATE to cut the list, or aggregate into a CLOB (XMLAGG was used above) when you need all values.

What does ...(170) at the end of LISTAGG mean?

With ON OVERFLOW TRUNCATE '...' WITH COUNT, Oracle appends the number of values it dropped. In the run above 170 codes were left out.

Why does || fail even though the column is VARCHAR2(4000)?

Each column fits, but the joined result is over 4000. Wrapping one side in TO_CLOB made the result a CLOB in the run above.

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