Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery - IFERROR for standardSQL

I'm about to migrate queries from Legacy to Standard in BQ. In Legacy I used to have x/y returning null if y=0, however, I'm stuck in StandardSQL with the error:

Error: division by zero

I'd like to apply something like IFERROR(x/y,null)

Is it available in StandardSQL?

like image 627
Ilja Avatar asked Mar 18 '18 09:03

Ilja


People also ask

How do you handle errors in BigQuery?

BigQuery hasn't documented it yet, but you can handle any type of exception in BigQuery by creating an exception handling clause, as described in the following example: BEGIN SELECT 1/0; EXCEPTION WHEN ERROR THEN SELECT @@error. message, @@error.

How do you write not equal to in BigQuery?

NOT EQUAL TO (!=) and EXISTS... EQUAL TO Giving Different Results.

How do you Unnest an array in BigQuery?

To convert an ARRAY into a set of rows, also known as "flattening," use the UNNEST operator. UNNEST takes an ARRAY and returns a table with a single row for each element in the ARRAY . Because UNNEST destroys the order of the ARRAY elements, you may wish to restore order to the table.

How do you call a function in Gbq?

Calling persistent user-defined functions (UDFs)After creating a persistent UDF, you can call it as you would any other function, prepended with the name of the dataset in which it is defined as a prefix. To call a UDF in a project other than the project that you are using to run the query, project_name is required.


1 Answers

In standard SQL you can use SAFE_DIVIDE(x, y)
It is an equivalent to the division operator (/). Returns NULL if an error occurs, such as division by zero.

like image 190
Mikhail Berlyant Avatar answered Sep 23 '22 14:09

Mikhail Berlyant