Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-30 · container image gvenzl/oracle-free:23-slim
VARCHAR2 uses the database character set (AL32UTF8 here) and NVARCHAR2 uses the national character set (AL16UTF16). When one expression has to return both, as in UNION, CASE or COALESCE, Oracle stops with ORA-12704 instead of picking one.
The fix is to make both sides NVARCHAR2: wrap the VARCHAR2 side in TO_NCHAR, or write string literals as N'…'. Check which columns are NVARCHAR2 in user_tab_columns. All three cases below were run on Oracle 26ai Free.
01UNION ALL of a VARCHAR2 column and an NVARCHAR2 column
customers.name is VARCHAR2 and global_customers.name is NVARCHAR2 (confirmed in user_tab_columns). SELECT id, name FROM customers UNION ALL SELECT id, name FROM global_customers fails with ORA-12704, and the * points at name in the first SELECT.
Changing the first branch to TO_NCHAR(name) returned both rows: Kim and Tanaka.
Setup
CREATE TABLE customers (id NUMBER PRIMARY KEY, name VARCHAR2(50));
CREATE TABLE global_customers (id NUMBER PRIMARY KEY, name NVARCHAR2(50));
INSERT INTO customers VALUES (1, 'Kim');
INSERT INTO global_customers VALUES (2, N'Tanaka');
COMMIT;Table created.
Table created.
1 row created.
1 row created.
Commit complete.SELECT id, name FROM customers
UNION ALL
SELECT id, name FROM global_customers;SELECT id, name FROM customers
*
ERROR at line 1:
ORA-12704: character set mismatch
Help: https://docs.oracle.com/error-help/db/ora-12704/COLUMN table_name FORMAT A16
COLUMN data_type FORMAT A10
SELECT table_name, data_type FROM user_tab_columns WHERE column_name = 'NAME' ORDER BY table_name;TABLE_NAME DATA_TYPE
---------------- ----------
CUSTOMERS VARCHAR2
GLOBAL_CUSTOMERS NVARCHAR2
2 rows selected.COLUMN name FORMAT A10
SELECT id, TO_NCHAR(name) AS name FROM customers
UNION ALL
SELECT id, name FROM global_customers
ORDER BY id; ID NAME
---------- ----------
1 Kim
2 Tanaka
2 rows selected.02A plain '…' literal in CASE with an NVARCHAR2 column
CASE WHEN name IS NULL THEN '(no name)' ELSE name END on an NVARCHAR2 column fails with ORA-12704. The * points at the literal.
DUMP shows why: '(no name)' is CharacterSet=AL32UTF8 (9 bytes), while N'(no name)' is CharacterSet=AL16UTF16 (18 bytes). Writing the literal as N'(no name)' fixed it, and the NULL row showed (no name).
Setup
CREATE TABLE global_customers (id NUMBER PRIMARY KEY, name NVARCHAR2(50), grade NUMBER);
INSERT INTO global_customers VALUES (1, N'Tanaka', 1);
INSERT INTO global_customers VALUES (2, NULL, 2);
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SELECT id, CASE WHEN name IS NULL THEN '(no name)' ELSE name END AS display_name FROM global_customers;SELECT id, CASE WHEN name IS NULL THEN '(no name)' ELSE name END AS display_name FROM global_customers
*
ERROR at line 1:
ORA-12704: character set mismatch
Help: https://docs.oracle.com/error-help/db/ora-12704/SELECT DUMP('(no name)', 1016) AS literal, DUMP(N'(no name)', 1016) AS n_literal FROM dual;LITERAL N_LITERAL
-------------------------------------------------------------- ----------------------------------------------------------------------------------
Typ=96 Len=9 CharacterSet=AL32UTF8: 28,6e,6f,20,6e,61,6d,65,29 Typ=96 Len=18 CharacterSet=AL16UTF16: 0,28,0,6e,0,6f,0,20,0,6e,0,61,0,6d,0,65,0,29
1 row selected.Adding N in front of the literal makes it NVARCHAR2 (AL16UTF16), the same as the column.
COLUMN display_name FORMAT A12
SELECT id, CASE WHEN name IS NULL THEN N'(no name)' ELSE name END AS display_name FROM global_customers ORDER BY id; ID DISPLAY_NAME
---------- ------------
1 Tanaka
2 (no name)
2 rows selected.03COALESCE of a VARCHAR2 column and an NVARCHAR2 column
In products, name_ko is VARCHAR2 and name_global is NVARCHAR2. COALESCE(name_ko, name_global) fails with ORA-12704; the * points at name_ko.
COALESCE(TO_NCHAR(name_ko), name_global) worked and returned Pencil and Note.
Setup
CREATE TABLE products (id NUMBER PRIMARY KEY, name_ko VARCHAR2(50), name_global NVARCHAR2(50));
INSERT INTO products VALUES (1, NULL, N'Pencil');
INSERT INTO products VALUES (2, 'Note', NULL);
COMMIT;Table created.
1 row created.
1 row created.
Commit complete.SELECT id, COALESCE(name_ko, name_global) AS name FROM products;SELECT id, COALESCE(name_ko, name_global) AS name FROM products
*
ERROR at line 1:
ORA-12704: character set mismatch
Help: https://docs.oracle.com/error-help/db/ora-12704/COLUMN column_name FORMAT A12
COLUMN data_type FORMAT A10
SELECT column_name, data_type FROM user_tab_columns WHERE table_name = 'PRODUCTS' ORDER BY column_id;COLUMN_NAME DATA_TYPE
------------ ----------
ID NUMBER
NAME_KO VARCHAR2
NAME_GLOBAL NVARCHAR2
3 rows selected.COLUMN name FORMAT A10
SELECT id, COALESCE(TO_NCHAR(name_ko), name_global) AS name FROM products ORDER BY id; ID NAME
---------- ----------
1 Pencil
2 Note
2 rows selected.FAQ
What does ORA-12704 character set mismatch mean?
One expression mixes VARCHAR2 (database character set) and NVARCHAR2 (national character set) values. Oracle does not choose one for you in UNION, CASE or COALESCE.
How do I UNION a VARCHAR2 and an NVARCHAR2 column?
Convert the VARCHAR2 side with TO_NCHAR(col) so both branches are NVARCHAR2. That worked on this page.
Why does a string literal cause ORA-12704?
A plain 'text' literal is in the database character set. Next to an NVARCHAR2 column in CASE, write it as N'text'.
How do I find which columns are NVARCHAR2?
SELECT column_name, data_type FROM user_tab_columns WHERE table_name = '…' shows VARCHAR2 or NVARCHAR2 for each column.
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