Presto checks if it is NULL and returns the default value (analogue of NVL)

Is there any analogue of NVL in the Presto database?

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

I solve it 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 anything that could simplify this code.

+16
source share
1 answer

The ISO SQL function for this is COALESCE

coalesce(my_field,0)

https://prestodb.io/docs/current/functions/conditional.html

PS COALESCEcan be used with several arguments. It will return the first (left) non-zero argument, or NULL if not found.

eg

coalesce (my_field_1,my_field_2,my_field_3,my_field_4,my_field_5)
+33
source

Source: https://habr.com/ru/post/1674290/


All Articles