DEV Community

Mer Go
Mer Go

Posted on

Oracle ORA-01400 when inserting an empty string: '' is NULL in Oracle

If you come to Oracle from MySQL, PostgreSQL or SQL Server, this one bites sooner or later. A customer has no phone number, so the app sends an empty string '', and the INSERT fails with ORA-01400: cannot insert NULL into.

In Oracle, a zero-length VARCHAR2 string is NULL. 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 where phone is NOT NULL:

CREATE TABLE customers (
  cust_id NUMBER PRIMARY KEY,
  name    VARCHAR2(20) NOT NULL,
  phone   VARCHAR2(20) NOT NULL
);
Enter fullscreen mode Exit fullscreen mode

The app sends '' to mean "no phone":

INSERT INTO customers (cust_id, name, phone) VALUES (1, 'KIM', '');
Enter fullscreen mode Exit fullscreen mode
INSERT INTO customers (cust_id, name, phone) VALUES (1, 'KIM', '')
                                                               *
ERROR at line 1:
ORA-01400: cannot insert NULL into ("LAB_A"."CUSTOMERS"."PHONE")
Enter fullscreen mode Exit fullscreen mode

You never passed NULL, yet Oracle says NULL.

Check: is '' really NULL?

SELECT CASE WHEN '' IS NULL THEN 'NULL' ELSE 'NOT NULL' END AS empty_string,
       NVL(LENGTH(''), -1) AS len
  FROM dual;
Enter fullscreen mode Exit fullscreen mode
EMPTY_ST        LEN
-------- ----------
NULL             -1
Enter fullscreen mode Exit fullscreen mode

'' IS NULL is true, and LENGTH('') is NULL too (shown as -1 through NVL). Oracle does not distinguish '' from NULL.

Fix: decide whether the column really needs a value

'' will never pass a NOT NULL constraint, so there are two options:

  • If the value is optional, allow NULL
  • If it is required, make the app send a real value

Here the phone is optional, so the column now allows NULL:

ALTER TABLE customers MODIFY phone NULL;
Enter fullscreen mode Exit fullscreen mode
Table altered.
Enter fullscreen mode Exit fullscreen mode

Same INSERT again:

INSERT INTO customers (cust_id, name, phone) VALUES (1, 'KIM', '');
SELECT cust_id, name, NVL(phone, '(null)') AS phone FROM customers;
Enter fullscreen mode Exit fullscreen mode
1 row created.


   CUST_ID NAME                           PHONE
---------- ------------------------------ --------------------
         1 KIM                            (null)
Enter fullscreen mode Exit fullscreen mode

It went in, but what is stored is NULL, not an empty string.

Summary

  • In Oracle, '' (a zero-length VARCHAR2) is NULL
  • '' into a NOT NULL column raises ORA-01400
  • '' IS NULL confirms it
  • Optional value: allow NULL. Required value: send a real one

I keep the other ORA-01400 causes (column left out, explicit NULL despite a DEFAULT, NULL into an identity column, and more), each reproduced and fixed on a real database, here:
https://errorfixlab.96ssg.com/en/ora-01400/

This post was written with the help of generative AI, based on output actually run in a test environment.

Top comments (0)