-- Select the first one which is not null:
example:
SQL> select coalesce(null,null,2) from dual;
COALESCE(NULL,NULL,2)
---------------------
2
-- Two parameters are only required here:
select nvl(1, null) from dual;
-- If equivalent then null else return the first param
select nullif(2,2) from dual;
The following three sections are equivalent:
-- One, notice the difference between CASE statement and CASE expression
SET SERVEROUTPUT ON
DECLARE
v_num1 NUMBER := &sv_num1;
v_num2 NUMBER := &sv_num2;
v_num3 NUMBER := &sv_num3;
v_result NUMBER;
BEGIN
v_result := CASE
WHEN v_num1 IS NOT NULL THEN v_num1
ELSE
CASE
WHEN v_num2 IS NOT NULL THEN v_num2
ELSE v_num3
END
END;
DBMS_OUTPUT.PUT_LINE ('Result: '||v_result);
END;
-- Two
CASE
WHEN v_num1 IS NOT NULL THEN v_num1
WHEN v_num2 IS NOT NULL THEN v_num2
ELSE v_num3
END
-- Three
CASE
WHEN v_num1 IS NOT NULL THEN v_num1
ELSE COALESCE(v_num2, v_num3)
END
example:
SQL> select coalesce(null,null,2) from dual;
COALESCE(NULL,NULL,2)
---------------------
2
-- Two parameters are only required here:
select nvl(1, null) from dual;
-- If equivalent then null else return the first param
select nullif(2,2) from dual;
The following three sections are equivalent:
-- One, notice the difference between CASE statement and CASE expression
SET SERVEROUTPUT ON
DECLARE
v_num1 NUMBER := &sv_num1;
v_num2 NUMBER := &sv_num2;
v_num3 NUMBER := &sv_num3;
v_result NUMBER;
BEGIN
v_result := CASE
WHEN v_num1 IS NOT NULL THEN v_num1
ELSE
CASE
WHEN v_num2 IS NOT NULL THEN v_num2
ELSE v_num3
END
END;
DBMS_OUTPUT.PUT_LINE ('Result: '||v_result);
END;
-- Two
CASE
WHEN v_num1 IS NOT NULL THEN v_num1
WHEN v_num2 IS NOT NULL THEN v_num2
ELSE v_num3
END
-- Three
CASE
WHEN v_num1 IS NOT NULL THEN v_num1
ELSE COALESCE(v_num2, v_num3)
END