I am looking for the detail answer with simple examples of these functions.
3 Answers
NVL checks if first argument is null and returns second argument:
select nvl(null, 'arg2') from dual
in this example result will be: arg2;
select nvl('arg1', 'arg2') from dual
and in this one: arg1;
NVL2 has different logic. If first argument is not null then NVL2 returns second argument, but in other case it will return third argument:
select nvl2('arg1', 'arg2', 'arg3') from dual
Result: arg2
select nvl2(null, 'arg2', 'arg3') from dual
Result: arg3
Nvl(arg1,arg2) nvl
is used for converting null values.
In nvl if argument 1 is null then it returns argument 2 but argument 1 is not null it returns itself.
In nvl2 (arg1,arg2,arg3)
in nvl2 it converts any number into according to given number with null also .
If arg 1 that's given number is null then it returns arg3
And on the other hand if it is not null it returns argument 2
Both the NVL(exp1, exp2) and NVL2(exp1, exp2, exp3) functions check the value exp1 to see if it is null.
With the NVL(exp1, exp2) function, if exp1 is not null, then the value of exp1 is returned; otherwise, the value of exp2 is returned, but case to the same data type as that of exp1.
With the NVL2(exp1, exp2, exp3) function, if exp1 is not null, then exp2 is returned; otherwise, the value of exp3 is returned.
nvl vs nvl2
contains the answer. At least for me, that is...