Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IsNull function in DB2 SQL?

Tags:

sql

null

db2

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
like image 650
Dave Avatar asked Sep 15 '08 17:09

Dave


People also ask

Is NULL in DB2 SQL?

DB2 supports null, and as such, you can use null to distinguish between a deliberate entry of 0 (for numerical columns) or a blank (for character columns) and an unknown or inapplicable entry (NULL for both numerical and character columns).

What is the Isnull function in SQL?

The ISNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.

IS NOT NULL in DB2?

The NOT NULL constraint is used to ensure that a given column of a table is never assigned the null value. Once a NOT NULL constraint has been defined for a particular column, any insert or update operation that attempts to place a null value in that column will fail.

What is NULL and NOT NULL in DB2?

The IS NULL condition is satisfied if the column contains a null value or if the expression cannot be evaluated because it contains one or more null values. If you use the IS NOT NULL operator, the condition is satisfied when the operand is column value that is not null, or an expression that does not evaluate to null.


2 Answers

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.

like image 113
MadMurf Avatar answered Oct 21 '22 23:10

MadMurf


In DB2 there is a function NVL(field, value if null).

Example:

SELECT ID, NVL(NAME, "Internal) AS NAME, NVL(PRICE,0) AS PRICE FROM PRODUCT WITH UR;

like image 5
Md. Kamruzzaman Avatar answered Oct 22 '22 00:10

Md. Kamruzzaman