Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Are these SQL statements equivalent – case when vs. NVL?

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)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading