Oracle empty string vs NULL
The one engine where '' and NULL are the same value, and how to write around it.
- Catégorie
- Conditionnel
- Returns
boolean- In this category
- 34 of 43
Syntaxe
x IS NULL | '' IS NULL
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| 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 | Oui | a zero-length VARCHAR2 IS NULL |
Exemples
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;
Bon à savoir
- 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.
Related on SQLly
Cheat sheets for the engines that have it: Oracle.
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.