Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 756
Sasha Shpota Avatar asked Apr 07 '17 10:04

Sasha Shpota


People also ask

IS null function in Presto?

Like most other functions in Presto, they return null if any argument is null. Note that in some other databases, such as PostgreSQL, they only return null if all arguments are null.

Is null and NVL?

NVL replaces a null with a string. NVL returns the replacement string when the base expression is null, and the value of the base expression when it is not null. To replace an expression with one value if it is null and a different value if it is not, use NVL2 .

What is coalesce in Presto?

COALESCE. coalesce(value1, value2[, ...]) Returns the first non-null value in the argument list. Like a CASE expression, arguments are only evaluated if necessary.


1 Answers

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) 
like image 97
David דודו Markovitz Avatar answered Oct 03 '22 14:10

David דודו Markovitz