Oracle and the iSeries: Understanding nulls

This user discovered that in converting an application from using Oracle 8i to using DB2/400, Oracle behaves differently when concatenation nulls.

In converting an application from using Oracle 8i to using DB2/400, I discovered that Oracle behaves differently when concatenation nulls.

In Oracle, when you concatenate a string with a NULL, you get the original string.

Example:

 SQL> select title,firstname,
  2  title || firstname
  3  from employee;

TITL FIRSTNAME                 TITLE||FIRSTNAME
---- ------------------------- -----------------------------
     Ed                        Ed

But, when doing the same thing on DB2/400, I found that concatenating anything to NULL returns NULL.

 Example:  
>  select title,firstname,
  title || firstname
  from employee;

title  firstName                  TITLE || FIRSTNAME
-      Ed                         -
 Therefore, Oracle acts differently than the iSeries on concatenating NULLs. On the iSeries, if either operand is NULL, the result is NULL. On Oracle, if either operand is NULL, the result is just the other operand. On the iSeries, we can use COALESCE (with an empty string) to produce the same result as Oracle.

 Example:  
> select title,firstname,
  coalesce(title,'') || firstname concatted
  from employee

title  firstName                  CONCATTED
-      Ed                         Ed

-------------------------------------------------------------------- 
According to the IBM SQL Reference manual: Chapter 2 -- Language Elements -- Expressions: With the Concatenation Operator "If either operand can be null, the result can be null, and if either is null, the result is the null value. Otherwise, the result consists of the first operand string followed by the second."


This was first published in July 2005

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchEnterpriseLinux

SearchDataCenter

Close