Search This Blog

Oracle/PLSQL: Coalesce Function


In Oracle/PLSQL, the coalesce function returns the first non-null expression in the list. If all expressions evaluate to null, then the coalesce function will return NULL.

The syntax for the coalesce function is:

COALESCE( expr1, expr2, ... expr_n )

For Example:
You could use the coalesce function in an SQL statement as follows:

SELECT COALESCE( address1, address2, address3 ) res FROM my_table;
The above coalesce statement is equivalent to the following IF-THEN-ELSE statement:

IF address1 IS NOT NULL THEN
    result := address1;
ELSIF address2 IS NOT NULL THEN
    result := address2;
ELSIF address3 IS NOT NULL THEN
    result := address3;
ELSE
    result := null;
END IF;

The coalesce function will compare each value, one by one and returns first non-null value.

No comments:

Post a Comment