tags:

views:

5876

answers:

5

Is there a performant equivalent to the isnull function for DB2?

Imagine some of our products are internal, so they don't have names:

Select product.id, isnull(product.name, "Internal) 
From product

Might return:

1 Socks 
2 Shoes 
3 Internal 
4 Pants
+6  A: 

You should use the coalesce function. See http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2.doc.sqlref/fcoal.htm

ljorquera
It works, thanks!
Dave
+1  A: 

I'm not familiar with DB2, but have you tried COALESCE?

ie:


SELECT Product.ID, COALESCE(product.Name, "Internal") AS ProductName
FROM Product
Chris Shaffer
It works, thanks!
Dave
A: 

COALESCE function same ISNULL function Note. you must use COALESCE function with same data type of column that you check is null.

Fuangwith S.
A: 
venkatram
+2  A: 

For what its worth, COALESCE is similiar but

IFNULL(expr1, default)

is the exact match you're looking for in DB2.

COALESCE allows multiple arguments, returning the first NON NULL expression, whereas IFNULL only permits the expression and the default.

Thus

SELECT product.ID, IFNULL(product.Name, "Internal") AS ProductName
FROM Product

Gives you what you're looking for as well as the previous answers, just adding for completeness.

MadMurf