Friday, June 09, 2006

COALESCE - DB2 function

The COALESCE function returns the value of the first nonnull expression.

The arguments are evaluated in the order in which they are specified, and the result of the function is the first argument that is not null. The result can be null only if all arguments can be null. The result is null only if all arguments are null.

Ex:
Assume that SCORE1 and SCORE2 are SMALLINT columns in table GRADES, and that nulls are allowed in SCORE1 but not in SCORE2. Select all the rows in GRADES for which SCORE1 + SCORE2 > 100, assuming a value of 0 for SCORE1 when SCORE1 is null.

SELECT * FROM GRADES WHERE COALESCE(SCORE1,0) + SCORE2 > 100;

No comments: