Presto check if NULL and return default (NVL analog)

[亡魂溺海] 提交于 2021-02-06 14:48:18

问题


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:


The ISO SQL function for that is COALESCE

coalesce(my_field,0)

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

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)


来源:https://stackoverflow.com/questions/43275356/presto-check-if-null-and-return-default-nvl-analog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!