user12625679
user12625679

Reputation: 696

Bigquery if null and 0 then condition

I would like to add a condition if the row value is null or 0 then 0 else 1.

SELECT CASE WHEN NUM_FRIEND IS NULL AND 0 THEN 0 ELSE 1 END AS friends_column
FROM TABLE

I get the following error:

No matching signature for operator and 

Upvotes: 0

Views: 4247

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269513

As a case expression, the syntax is:

SELECT (CASE WHEN NUM_FRIEND IS NULL OR NUM_FRIEND = 0 THEN 0 ELSE 1 END) AS friends_column
FROM TABLE;

You can simplify this to:

SELECT (CASE WHEN NUM_FRIEND <> 0 THEN 1 ELSE 0 END) AS friends_column
FROM TABLE;

Or, instead of 0 and 1, return a boolean:

SELECT (NUM_FRIEND <> 0) AS friends_column
FROM TABLE;

Upvotes: 1

Related Questions