// conditional reference

Oracle empty string vs NULL

The one engine where '' and NULL are the same value, and how to write around it.

Категория
Условные конструкции
Returns
boolean
In this category
34 of 43

Синтаксис

x IS NULL | '' IS NULL

Поддержка движка

ДвижокSupportЗаметки
SQL Server Not built in No built-in equivalent.
PostgreSQL Not built in No built-in equivalent.
MySQL Not built in No built-in equivalent.
SQLite Not built in No built-in equivalent.
Oracle Да a zero-length VARCHAR2 IS NULL

Примеры

Oracle

SELECT CASE WHEN '' IS NULL THEN 'yes' ELSE 'no' END AS empty_is_null FROM dual;

PostgreSQL

SELECT CASE WHEN '' IS NULL THEN 'yes' ELSE 'no' END AS empty_is_null;

Полезно знать

  • Oracle: '' IS NULL is TRUE, so NVL(col, '') is a no-op and CONCAT(col, '') can still be NULL.
  • PostgreSQL, MySQL, SQL Server and SQLite all keep '' and NULL distinct; code that works on Oracle may mis-handle empties elsewhere and vice versa.
  • Fixed-length CHAR columns pad with spaces, so a single space stored in CHAR(5) is a real value, not NULL: a second kind of 'empty-looking' row.
  • WHERE col = '' never matches in Oracle, because it is col = NULL; write WHERE col IS NULL. If 'known to be empty' and 'unknown' must stay apart, store that in a separate flag column.

Questions about NULL

Is an empty string NULL in SQL?

In Oracle yes; in PostgreSQL, MySQL, SQL Server and SQLite no. They are distinct values.

Why does my Oracle INSERT store NULL?

You inserted ''; Oracle converts zero-length VARCHAR2 values to NULL.

How do I represent 'no value yet' portably?

Use NULL and IS NULL checks everywhere. Code that needs a distinct 'empty' value cannot rely on '' if Oracle is a target.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.