The solution is not "coalesce" but rater NVL. If you knew beforehand that the other two values are, for example, positive numbers, then you could do something like greatest(1, NVL(null, -1), 2)
- or, more likely, if you have three numeric columns, any one of which could be NULL, and you knew all values must be positive numbers, you could do something like greatest(NVL(col1, -1), NVL(col2, -1), NVL(col3, -1))
- and then, if the result is -1, you would know all three values were NULL.
However, if you allow NULL that probably means you may, sometimes, have values that are not known. Is it not possible that the greatest among your values is in fact one of these unknown values? (That is, isn't NULL the correct answer for greatest()
in that case?)
ADDED: You can always avoid coalesce
and NVL
, for example instead of NVL(col1, -1)
you can write case when col1 is null then -1 else col1 end
- but WHY???