Skip to main content

Command Palette

Search for a command to run...

Generating date intervals in BigQuery

Updated
1 min read
Generating date intervals in BigQuery
C

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified

Ever had to generate a date interval in BigQuery?

Take a look at the GENERATE_DATE_ARRAY function.

Needs 3 arguments:
- start_date
- end_date
- interval step (DAY, WEEK, MONTH, QUARTER, YEAR)

Since it generates an ARRAY, we would need to UNNEST it to get one date per row.

If you need something more granular, there is the very similar GENERATE_TIMESTAMP_ARRAY, which can generate in increments between MICROSECOND and DAY.

Friendly reminder to not mix and match DATETIME and TIMESTAMP without properly converting between them beforehand - see my previous post.

Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.

Practical SQL

Part 1 of 50

Short, practical posts on SQL and BigQuery — from core language features to advanced query patterns. A reference for data practitioners at every level.

More from this blog

D

Datawise — SQL, BigQuery & Python for Data Engineers

205 posts

Data Engineer with a passion for transforming complex data landscapes into insightful stories. Here on my blog, I share insights, challenges, and the ever-evolving dance of technology and business.