Bogo
Bogo

Reputation: 688

Oracle: best way to deal with NULL values

I have this piece of code within a procedure:

 if val is NULL
    then 
    creb :=0;
   else 
     creb := val;
 end if;

Is there more elegant way to deal with null?

Upvotes: 1

Views: 98

Answers (2)

Sylvain Leroux
Sylvain Leroux

Reputation: 52070

As a complement to Bohemian's answer, Oracle has the non-standard NVL -- and the NVL2 if you need an else case with a value different from the condition -- functions to deal with that issue.

creb := nvl(val, 0);

COALESCE has both advantages of being standard and accepting multiple arguments.

Upvotes: 5

Bohemian
Bohemian

Reputation: 425448

Use coalesce (), which returns the first non-null value in the list if values passed to it:

creb := coalesce(val, 0);

Upvotes: 6

Related Questions