Presto Check If Null and Return Default (Nvl Analog)

Is there any analog of NVL in Presto DB?

I need to check if a field is NULL and return a default value.

I solve this somehow like this:

SELECT
  CASE 
    WHEN my_field is null THEN 0 
    ELSE my_field 
  END
FROM my_table

But I'm curious if there is something that could simplify this code.

1 Answer

The ISO SQL function for that is COALESCE

coalesce(my_field,0)

P.S. COALESCE can be used with multiple arguments. It will return the first (from the left) non-NULL argument, or NULL if not found.

e.g.

coalesce (my_field_1,my_field_2,my_field_3,my_field_4,my_field_5)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest