HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Oracle empty string converts to null

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
convertsnullemptyoraclestring

Problem

When doing an insert, an empty string is converted to null:

insert into test (f) values ('');


Now, there is a row with f containing a null.

But, when I query the table, I cannot use '':

select * from test where f='';
no rows selected


I can use null:

select * from test where f is null;
____F_
NULL


So... it appears that Oracle decided that empty strings cannot be used for insert, but they remain empty strings when doing queries. Where is the documentation on when an empty string becomes a null and when it remains an empty string?

Solution

This says it all:

select NVL('','it is null') as value
from dual;


SQL Fiddle

2 things:

1) '' gets converted to NULL on insert. That's an Oracle VARCHAR2 thing.

2) select from test where f=''; is trying to do select from test where f=NULL, which isn't defined, and will return nothing because NULL doesn't like the equality operator. You have to use IS NULL or IS NOT NULL.

I'll add that the CHAR datatype behaves differently because it is padded.

Code Snippets

select NVL('','it is null') as value
from dual;

Context

StackExchange Database Administrators Q#49744, answer score: 11

Revisions (0)

No revisions yet.