Presto检查是否为NULL并返回默认值(NVL模拟) [英] Presto check if NULL and return default (NVL analog)
本文介绍了Presto检查是否为NULL并返回默认值(NVL模拟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Presto DB中是否有NVL的类似物?
Is there any analog of NVL in Presto DB?
我需要检查字段是否为NULL并返回默认值.
I need to check if a field is NULL and return a default value.
我这样解决这个问题:
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.
推荐答案
ISO SQL函数是 COALESCE
The ISO SQL function for that is COALESCE
coalesce(my_field,0)
https://prestodb.io/docs/current/functions/conditional.html
P.S. COALESCE
可以与多个参数一起使用.它将返回第一个(从左开始)非NULL参数,或者返回NULL(如果找不到).
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 (my_field_1,my_field_2,my_field_3,my_field_4,my_field_5)
这篇关于Presto检查是否为NULL并返回默认值(NVL模拟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文