Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle NVL with empty string

Tags:

sql

null

oracle

nvl

I have this table where NULL is the NULL value, not the string NULL:

MYCOL
--------
NULL
example

Why does this query not return the NULL row?

select * from example_so where nvl(mycol, '') = '';
like image 920
kmkaplan Avatar asked Oct 09 '14 09:10

kmkaplan


People also ask

Is empty string considered null in Oracle?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.

Can we use NVL on string?

NVL replaces a null with a string. NVL returns the replacement string when the base expression is null, and the value of the base expression when it is not null. To replace an expression with one value if it is null and a different value if it is not, use NVL2 .

Can NVL return null?

NVL. The NVL function allows you to replace null values with a default value. If the value in the first parameter is null, the function returns the value in the second parameter. If the first parameter is any value other than null, it is returned unchanged.

Is null and blank same in Oracle SQL?

In database terms, however, a null value is a value that doesn't exist: the field does not contain a value of any kind (not even a blank value). By contrast, a blank value is a real value: it just happens to be a string value containing 0 characters.


1 Answers

'' is again NULL in Oracle, because Oracle doesn't support empty Strings just like Other High Level languages or DBMS..

You need to look for NULL/empty string using IS NULL or IS NOT NULL

No other relational operator work against NULL, though it is syntactically valid. SQLFiddle Demo

It has to be,

select * from example_so where mycol IS NULL

EDIT: As per Docs

Oracle Database currently treats a character value with a length of zero as null. However, this may not continue to be true in future releases, and Oracle recommends that you do not treat empty strings the same as nulls.

like image 183
Maheswaran Ravisankar Avatar answered Sep 22 '22 17:09

Maheswaran Ravisankar