Eric Hendershott
Eric Hendershott

Reputation: 643

BigQuery SQL WHERE Date Between Current Date and -15 Days

I am trying to code the following condition in the WHERE clause of SQL in BigQuery, but I am having difficulty with the syntax, specifically date math:

WHERE date_column between current_date() and current_date() - 15 days

This seems easy in MySQL, but I can't get it to work with BigQuery SQL.

Upvotes: 37

Views: 130730

Answers (3)

ppk28
ppk28

Reputation: 377

This works for me.

WHERE DATE(date_column) BETWEEN DATE(DATE_ADD(CURRENT_DATE(), -15, 'DAY'))
AND CURRENT_DATE()

Upvotes: 2

JohnHC
JohnHC

Reputation: 11195

Use DATE_SUB

select * 
from TableA
where Date_Column between DATE_SUB(current_date(), INTERVAL 15 DAY) and current_date()

Remember, between needs the oldest date first

Upvotes: 55

Siyual
Siyual

Reputation: 16917

You should probably switch the two around - the syntax should be the following:

WHERE date_column BETWEEN DATE_ADD(CURRENT_DATE(), -15, 'DAY') AND CURRENT_DATE()

Upvotes: 13

Related Questions