patternModerate
Oracle empty string converts to null
Viewed 0 times
convertsnullemptyoraclestring
Problem
When doing an insert, an empty string is converted to null:
Now, there is a row with f containing a null.
But, when I query the table, I cannot use '':
I can use 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?
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 selectedI can use null:
select * from test where f is null;
____F_
NULLSo... 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:
SQL Fiddle
2 things:
1)
2)
I'll add that the
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.