Home > Back-end >  Are these SQL statements equivalent - case when vs. NVL?
Are these SQL statements equivalent - case when vs. NVL?

Time:01-10

My colleague has the following SQL statement executed in Redshift:

SELECT
    CASE
        WHEN P.PRED_VAL IS NULL THEN R.SCORE_VAL
        ELSE P.PRED_VAL
    END AS FINAL_SCORE
FROM RESULT R
INNER JOIN PREDICTION P
    ON R.ID = P.ID;

Is there any context in which the result of that statement wouldn't be the same as NVL(P.PRED_VAL, R.SCORE_VAL)?

CodePudding user response:

nvl is simply a non-standard name for the SQL standard coalesce function.

There is no functional difference between your case and your nvl, except that nvl is non-standard. Use coalesce.

  • Related