Are there non obvious differences between NVL and Coalesce in Oracle?
The obvious differences are that coalesce will return the first non null item in its parameter list whereas nvl only takes two parameters and returns the first if it is not null, otherwise it returns the second.
It seems that NVL may just be a ‘Base Case” version of coalesce.
Am I missing something?
COALESCEis more modern function that is a part ofANSI-92standard.NVLisOraclespecific, it was introduced in80‘s before there were any standards.In case of two values, they are synonyms.
However, they are implemented differently.
NVLalways evaluates both arguments, whileCOALESCEusually stops evaluation whenever it finds the first non-NULL(there are some exceptions, such as sequenceNEXTVAL):This runs for almost
0.5seconds, since it generatesSYS_GUID()‘s, despite1being not aNULL.This understands that
1is not aNULLand does not evaluate the second argument.SYS_GUID‘s are not generated and the query is instant.