25

I want to make calculation among columns, which contains null values

x1     x2
9      0.0
5      1.2
12     null
10     null

If calculation

x1 + (x1*x2)

is made, it results in

9, 6, null, null

Can you pls suggest, how null values can be handled, so the result will be

9, 6, 12, 10

I was trying ifelse, if value is null, then use 1

IF(x1 = null, 0, x1)

but the results is still with null values.

Thank you!

0

3 Answers 3

46

Use IFNULL(expr, 0) - this will come back as 0 if expr is null.

In general, instead of doing something=null do something IS null.

Sign up to request clarification or add additional context in comments.

1 Comment

And the reason is that any expression involving null (aside from "IS NULL" and "IFNULL") will evaluate to null, even if that expression is something like "foo = null".
7

Use ifnull function in Google Big Query. (Link provided)

IFNULL(x1, 0)*IFNULL(x2,0) as new_col 
  • This will plug in 0 in place of any null values in x1 and x2.

FYI: There is a ISNULL(column_name) in Google cloud Data Prep just like in MySQL that will return a boolean i.e either True or False depending upon the column is null or not.

Comments

2

Another possible solution is using COALESCE(arg1, arg2, ...,argN) Which returns the first not null argument.

For example:

COALESCE(x1 , 0) * COALESCE(x2, 0) returns 0 if x1 (or x2) is null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.