ORA-12899 for a 4-character name in VARCHAR2(10)? Byte vs char length semantics
A VARCHAR2(10) column, a 4-character name, and Oracle says ORA-12899: value too large for column ... (actual: 12, maximum: 10).
If your app stores Korean, Japanese, Chinese, or any other non-ASCII text, you will hit this sooner or later. The cause: the column length counts bytes, not characters.
I reproduced it on Oracle AI Database 26ai Free (23.26.3) and ran the check and the fix. All output below is real SQL*Plus output.
Reproduce
A table with a VARCHAR2(10) name column, and a 4-character Korean name (written with UNISTR so the encoding is unambiguous).
CREATE TABLE members (member_id NUMBER PRIMARY KEY, name VARCHAR2(10));
INSERT INTO members VALUES (1, UNISTR('\AE40\C218\D55C\AE38'));
INSERT INTO members VALUES (1, UNISTR('\AE40\C218\D55C\AE38'))
*
ERROR at line 1:
ORA-12899: value too large for column "LAB_A"."MEMBERS"."NAME" (actual: 12, maximum: 10)
Help: https://docs.oracle.com/error-help/db/ora-12899/
Four characters, but "actual: 12". That 12 is not a character count.
Check 1: the column definition
COLUMN column_name FORMAT A12
SELECT column_name, data_length, char_length, char_used FROM user_tab_columns WHERE table_name = 'MEMBERS' AND column_name = 'NAME';
SELECT value FROM nls_session_parameters WHERE parameter = 'NLS_LENGTH_SEMANTICS';
COLUMN_NAME DATA_LENGTH CHAR_LENGTH C
------------ ----------- ----------- -
NAME 10 10 B
1 row selected.
VALUE
----------------------------------------------------------------
BYTE
1 row selected.
CHAR_USED is B (BYTE). VARCHAR2(10) means 10 bytes here, and the session's NLS_LENGTH_SEMANTICS is the default, BYTE.
Check 2: character set and actual byte length
SELECT value FROM nls_database_parameters WHERE parameter = 'NLS_CHARACTERSET';
SELECT LENGTH(TO_CHAR(UNISTR('\AE40\C218\D55C\AE38'))) AS chars, LENGTHB(TO_CHAR(UNISTR('\AE40\C218\D55C\AE38'))) AS bytes FROM dual;
VALUE
----------------------------------------------------------------
AL32UTF8
1 row selected.
CHARS BYTES
---------- ----------
4 12
1 row selected.
The database character set is AL32UTF8. These 4 characters take 3 bytes each, 12 bytes in total, which does not fit in 10 bytes.
Fix: define the column in characters
ALTER TABLE members MODIFY name VARCHAR2(10 CHAR);
Table altered.
Running the same INSERT again:
1 row created.
VARCHAR2(10 CHAR) means 10 characters, so the 4-character name fits.
Takeaways
- Without a qualifier,
VARCHAR2(n)is measured in bytes (NLS_LENGTH_SEMANTICS = BYTE) - In
AL32UTF8, non-ASCII characters take more than one byte, so you hit the limit before n characters user_tab_columns.char_usedshowsBorC- For columns that hold non-ASCII text, use
VARCHAR2(n CHAR)
Other causes of ORA-12899 (appending in an UPDATE, INSERT ... SELECT from a wider staging table, a trigger writing to a history table) are on this page, each reproduced, checked, and fixed on a real database: https://errorfixlab.96ssg.com/en/ora-12899/
This article was written with the help of generative AI, based on output actually run in a test environment.
Comments
No comments yet. Start the discussion.