Skip to content
Advertisement

Filtering ‘Where In’ Queries by ‘Is Not Null’ in Big Query

I am attempting to query a table in BigQuery programmatically, and have a WHERE-IN clause that I would like to support having NULL as the value. Current Query looks like:

SELECT * FROM DATASET.TABLE_NAME WHERE HIT_DATE = 'YYYY-MM-DD' AND ID IN (LIST_OF_IDS);

I am wondering if there is a way so that I can query everything from the table for a specific date in the case that the LIST_OF_IDS input is NULL (The idea being that I want to support choosing between an inputting of a list of ids or return everything if no list of ids are given).

I tried

SELECT * FROM DATASET.TABLE_NAME WHERE HIT_DATE = 'YYYY-MM-DD' AND ID IN (LIST_OF_IDS) IS NOT NULL;

But got this error thrown at me:

Syntax error: Expression to the left of IS must be parenthesized

Advertisement

Answer

If I understand correctly you are looking for this :

SELECT * 
FROM DATASET.TABLE_NAME 
WHERE HIT_DATE = 'YYYY-MM-DD' 
AND (ID IN (LIST_OF_IDS) OR LIST_OF_IDS IS NULL);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement