Presto检查是否为NULL并返回默认值(NVL模拟)

时间:2022-01-01 00:59:38

Is there any analog of NVL in presto?

在presto中是否有任何类似的NVL?

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

我需要检查字段是否为NULL并返回默认值。

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 something that could simplify this code.

但我很好奇是否有可以简化此代码的东西。

My driver version is 0.171

我的驱动版本是0.171

1 个解决方案

#1


9  

The ISO SQL function for that is COALESCE

ISO SQL函数就是COALESCE

coalesce(my_field,0)

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

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.

附: COALESCE可以与多个参数一起使用。它将返回第一个(从左侧)非NULL参数,如果未找到则返回NULL。

e.g.

例如

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

#1


9  

The ISO SQL function for that is COALESCE

ISO SQL函数就是COALESCE

coalesce(my_field,0)

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

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.

附: COALESCE可以与多个参数一起使用。它将返回第一个(从左侧)非NULL参数,如果未找到则返回NULL。

e.g.

例如

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