The art of doing mathematics consists in finding that special case which contains all the germs of generality.
David Hilbert
One of the most mindboggling values in the Oracle database is the NULL value. What is NULL? NULL is nothing. NULL is not even the same as NULL. NULL is undefined. But you need to work with NULL values (which are no actual values). Luckily Oracle provides us with a couple of functions to do the heavy lifting when it comes to checking for NULLs
IS [NOT] NULL
Syntax:
expr1 IS [NOT] NULL
This predicate returns rows from the table where the column value for expr1 contains (or
doesn’t contain) a NULL value.
| EMPNO | ENAME | JOB | SAL | COMM | |
| 1 | 7876 | ADAMS | CLERK | 1100.00 | |
| 2 | 7499 | ALLEN | SALESMAN | 1600.00 | 300.00 |
| 3 | 7839 | KING | PRESIDENT | 5000.00 | |
| 4 | 7654 | MARTIN | SALESMAN | 1250.00 | 1400.00 |
| 5 | 7369 | SMITH | CLERK | 800.00 | |
| 6 | 7844 | TURNER | SALESMAN | 1500.00 | 0.00 |
If you want to see all the employees with no commission filled in you can issue a statement like this:
SELECT * FROM emp t WHERE t.comm IS NULL;
| EMPNO | ENAME | JOB | SAL | COMM | |
| 1 | 7876 | ADAMS | CLERK | 1100.00 | |
| 2 | 7839 | KING | PRESIDENT | 5000.00 | |
| 3 | 7369 | SMITH | CLERK | 800.00 |
Alternatively you can find the employees with a value in the commission column like this:
SELECT * FROM emp t WHERE t.comm IS NOT NULL;
| EMPNO | ENAME | JOB | SAL | COMM | |
| 2 | 7499 | ALLEN | SALESMAN | 1600.00 | 300.00 |
| 4 | 7654 | MARTIN | SALESMAN | 1250.00 | 1400.00 |
| 6 | 7844 | TURNER | SALESMAN | 1500.00 | 0.00 |
Notice NULL is not the same as 0 (zero). NULL is not even the same as NULL. NULL = NULL results in FALSE where NULL IS NULL results in TRUE
NVL
Syntax:
NVL(expr1, expr2)
If expr1 contains a NULL value, then replace it with the value of expr2
The NVL function lets you substitute a value when a null value is encountered.
Examples:
NVL(‘A’,’B’) results in A
NVL(NULL,’B’) results in B
NVL(1,2) results in 1
NVL(NULL,2) results in 2
NVL(‘A’,2) results in A
NVL(1, ‘B’) results in an error
The last example results in an error because ‘B’ cannot be converted to a number. In the
one before it, it was possible to convert 2 to a varchar2 value.
NVL2
Syntax:
NVL2(expr1, expr2, expr3)
If expr1 contains a NULL value, then return expr3. If the value of expr1 contains a non-NULL
value, then return expr2.
Examples:
NVL2(‘A’,’B’,’C’) results in B
NVL2(NULL,’B’,’C’) results in C
NVL2(1,2,3) results in 2
NVL2(NULL,2,3) results in 3
NULLIF
Syntax:
NULLIF(expr1, expr2)
NULLIF returns NULL if expr1 is equal to expr2. If they are not equal expr1 is returned.
Expressions must be of the same data type, There is no implicit conversion performed.
Examples:
NULLIF(‘A’,’B’) results in A
NULLIF(‘A’,’A’) results in NULL
NULLIF(2,3) results in 2
NULLIF(2,2) results in NULL
NULLIF(‘2’,2) results in an error
NULLIF(2,’2’) results in an error
COALESCE
Syntax:
COALESCE(expr [, expr ]...)
The coalesce function returns the first non-NULL value of the expressions in the list. The list
must consist of at least 2 values. If all expressions evaluate to NULL then NULL is returned.
Examples:
COALESCE(‘A’,’B’,’C’) results in A
COALESCE(NULL,’B’,’C’) results in B
COALESCE(NULL,NULL,’C’) results in C
COALESCE(NULL,’B’,NULL) results in B
COALESCE(‘A’) results in an error
LNNVL
Syntax:
LNNVL(condition)
The LNNVL function is used in the WHERE clause of an SQL statement when one of the
operands may contain a NULL value. The function returns TRUE is the result of the
condition is FALSE and FALSE is the result of the condition is TRUE or UNKNOWN. LNNVL
can be used in a condition when you would otherwise need to combine a condition with an
IS [NOT] NULL or an NVL predicate.
The following queries have the same result:
SELECT * FROM emp e WHERE LNNVL(e.comm >= 100); SELECT * FROM emp e WHERE 1 = 1 AND ((e.comm
Note that the condition must be inverted when using the LNNVL function.
DEFAULTS
When a column in a table is defined there is the possibility to have a default value for this column whenever it is inserted without a value.
create table def ( code number , value varchar2(30) default 'Empty' )
This is done on insert only. You can still update the column to be NULL (or insert a NULL value) when by supplying an explicit NULL value.
insert into def (code) values (10) select * from def insert into def (code, value) values (15, NULL) update def set value = null where code = 10 select * from def
If you modify the default during the life of the table, the new default will be applied from that
moment on.
alter table DEF modify value default 'Really empty'; insert into def (code) values (20)
Now how do you make sure a default is applied to columns already NULL? You can do this
by using the keyword DEFAULT.
update def set value = default where value is null select * from def
DECODE
Syntax:
DECODE(expr, search, result [, search, result ]... [, default ])
Using DECODE you can the value of a column (or any expression) to decide what to return. It can for instance be used to build a decision table. Consider the following decision table:
| Code | Value |
| 1 | Red |
| 2 | White |
| 3 | Blue |
| 4 | Yellow |
The column in our table just holds the code instead of the value. If we want to represent the value instead of the code we could write a query like this:
SELECT decode(color, 1, 'Red', 2, 'White', 3, 'Blue', 4, 'Yellow') FROM table;
CASE
Syntax:
CASE { simple_case_expression
| searched_case_expression
}
[ else_clause ]
END
simple_case_expression
expr
{ WHEN comparison_expr THEN return_expr }...
searched_case_expression
{ WHEN condition THEN return_expr }...
else_clause
ELSE else_expr
The case statement has two flavors. The simple case and the searched case. In the simple case the expression is written only once and depending on the result of this expression one of the possible cases is being used. This can be helpful when you need to choose from a set of distinct values. In the searched case every case has its own expression. This can for instance be helpful when using ranges.
The same query as above can be written using a simple case expression.
SELECT CASE color
WHEN 1 THEN 'Red'
WHEN 2 THEN 'White'
WHEN 3 THEN 'Blue'
WHEN 4 THEN 'Yellow'
ELSE 'Unknown'
END color
FROM table;
The query can also be written using a searched case expression:
SELECT CASE
WHEN color=1 THEN 'Red'
WHEN color=2 THEN 'White'
WHEN color=3 THEN 'Blue'
WHEN color=4 THEN 'Yellow'
END color
FROM table;
The else clause is optional. If none of the cases is valid for the expression, then NULL is
returned, as opposed to PL/SQL where an error is raised.
Conclusion
NULL values are one of the most mind-boggling values in the Oracle database. Luckily Oracle provides us with a lot of functionality to work with NULLs. The newer versions of the database also provide us with functionality to make your SQL (and PL/SQL) code more readable, for instance the CASE keyword which can replace the DECODE keyword and has more possibilities as well.
ref:
http://oracleworldblog.blogspot.nl/2009/10/oracle-null-functions.html
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions119.htm
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions120.htm
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions116.htm
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions030.htm
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions091.htm
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions049.htm
http://docs.oracle.com/cd/E11882_01/server.112/e26088/expressions004.htm











7 Comments
Norman Dunbar
30/08/2012
I once received a check constraint something like the following from a vendor, all allegedly tested:
check (sex in ('M','F',NULL))
Guess what happens with a constraint like that then? Which of the following are allowed:
'M', 'm', 'F', 'f' NULL, 'X', 'x', '6', '7', '8', '#'
All of them are. Try it out!
I think the databases was running at 9i. I haven't tested this on 10g or 11g yet, I might one day, but suffice to say the vendor got it sent right back at him with a request to properly test patches they were sending us!
Cheers,
Norm.
Norman Dunbar
30/08/2012
Ok, just tetsed, still works in 11g:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning option
SQL> create table sex(mf char(1) check (mf in ('M','F',NULL)));
Table created.
SQL> insert into sex values (null);
1 row created.
SQL> insert into sex values ('M');
1 row created.
SQL> insert into sex values ('F');
1 row created.
SQL> insert into sex values ('7');
1 row created.
SQL> commit;
Commit complete.
SQL> select nvl(mf, 'NULL') from sex;
NVL(
----
NULL
M
F
7
Cheers,
Norm.
Jan Leers
30/08/2012
What Oracle is doing is not wrong. It's like:
SELECT 1 FROM dual WHERE 1 NOT IN (2, NULL);
The best description for NULL is "unknown", in this select the NULL could well be a 1, we don't know. In the constraint, it could be a 7, not sure, but it could.
The correct syntax off course is check (sex in ('M','F')), and leave the column nullable.
Norman Dunbar
30/08/2012
Hi Jan,
yes, I know what Oracle is doing! ;-) But my vendor didn't understand how NULL works, and obviously, never tested the constraint.
What does irritate me about Oracle's NULL handling is that an empty string is considered to be NULL, and as such, cannot be compared with itself. Whereas an empty LOB is not considered NULL.
Cheers,
Norm.
Christoph Ruepprich
30/08/2012
Great post. I keep forgetting about nvl2. I'll have to stick a post-it somewhere reminding me of it.
C
Alex Tokarev
05/09/2012
I think it would be good to mention DECODE permits to use something like DECODE(null, 2, 3) and it compares null with null correctly, not like NULL = NULL comparison in CASE statement for instance.
Patrick Barel
16/09/2012
Alex,
I think you should never expect NULL = NULL to be TRUE. If you would want a comparison with NULL then you can use something like this:
SELECT CASE WHEN color IS NULL THEN 'Red' WHEN color=2 THEN 'White' WHEN color=3 THEN 'Blue' WHEN color=4 THEN 'Yellow' END color FROM table;No trackbacks yet.