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
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;Table created.
500 rows created.
Commit complete.SELECT LISTAGG(code, ',') WITHIN GROUP (ORDER BY id) AS codes FROM items;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/SELECT COUNT(*) AS cnt, SUM(LENGTHB(code) + 1) AS total_bytes FROM items; CNT TOTAL_BYTES
---------- -----------
500 6000
1 row selected.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; 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
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;Table created.
500 rows created.
Commit complete.SELECT LISTAGG(code, ',') WITHIN GROUP (ORDER BY id) AS codes FROM items;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/SELECT COUNT(*) AS cnt FROM items; CNT
----------
500
1 row selected.SELECT DBMS_LOB.GETLENGTH(XMLCAST(XMLAGG(XMLELEMENT(e, code || ',') ORDER BY id) AS CLOB)) AS clob_len FROM items; 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
CREATE TABLE notes (id NUMBER PRIMARY KEY, body VARCHAR2(4000));
INSERT INTO notes VALUES (1, RPAD('A', 3000, 'A'));
COMMIT;Table created.
1 row created.
Commit complete.SELECT LENGTH(body || RPAD('B', 2000, 'B')) AS len FROM notes;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/SELECT LENGTH(body) AS len FROM notes; LEN
----------
3000
1 row selected.SELECT LENGTH(TO_CLOB(body) || RPAD('B', 2000, 'B')) AS len FROM notes; 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