Google BigQuery SQL Dates and Times Cheat Sheet
By: Rebecca Vickery
Tags: Programming, Bigquery, Google Big Query, Sql, Data Science

I have just started working on a time series forecasting project this morning. The first step in this project, as with most data science tasks, was to gather the data first. This meant a relatively simple SQL query in Google BigQuery. Once again I found myself googling how to extract the year from a timestamp, and scrolling through the documentation to find the correct function and realised that I needed to write this down somewhere. I am writing this into a blog post so that I have something to refer back to, and hopefully this may also help others. Please note all expressions are based on Standard SQL.

Dates

Date parts

List of all date parts that can be used in the expressions listed below:

DAYOFWEEK (returns 1-7 Sunday is 1)

DAY

DAYOFYEAR (0-365)

WEEK (week of year 0-53, week begins on Sunday)

WEEK(<chosen weekday>) (week of year begins on your chosen day e.g. SUNDAY)

ISOWEEK (ISO 8601 week number, week begins on Monday)

MONTH

QUARTER (1-4)

YEAR (ISO 8601 year number)

Extract a date part

EXTRACT(part FROM date_expression) Example: EXTRACT(YEAR FROM 2019-04-01) Output: 2019

Construct a date from integers

DATE(year, month, day) Example: DATE(2019, 04, 01) Output: 2019-04-01

Adding and subtracting from dates

DATE_ADD(date_expression, INTERVAL INT64_expr date_part) Example: DATE_ADD('2019-04-01', INTERVAL 1 DAY) Output: 2019-04-02 DATE_SUB(date_expression, INTERVAL INT64_expr date_part) Example: DATE_SUB('2019-04-01', INTERVAL 1 DAY) Output: 2019-03-31 Example use case - dynamic dates: where my_date between DATE_SUB(current_date, INTERVAL 7 DAY) and DATE_SUB(current_date, INTERVAL 1 DAY)

Difference between two dates

DATE_DIFF(date_expression, date_expression, date_part) Example: DATE_DIFF(2019-02-02, 2019-02-01, DAY) Output: 1

Specify the granularity of a date

DATE_TRUNC(date_expression, date_part) Example: DATE_TRUNC(2019-04-12, WEEK) Output: 2019-04-07

Times

Time parts

MICROSECOND

MILLISECOND

SECOND

MINUTE

HOUR

Construct a datetime object from integers

DATETIME(year, month, day, hour, minute, second)

DATETIME(date_expression, time_expression)

DATETIME(timestamp_expression [, timezone]) Example: DATETIME(2019, 04, 01, 11, 55, 00) Output: 2019-04-01 11:55:00

Add and subtract times

DATETIME_ADD(datetime_expression, INTERVAL INT64_expr part) Example: DATETIME_ADD('2019-04-01 11:55:00', INTERVAL 1 MINUTE) Output: 2019-04-01 11:56:00 DATETIME_SUB(datetime_expression, INTERVAL INT64_expr part) Example: DATETIME_SUB('2019-04-01 11:55:00', INTERVAL 1 MINUTE) Output: 2019-04-01 11:54:00

Difference between two times

DATETIME_DIFF(datetime_expression, datetime_expression, part) Example: DATETIME_DIFF('2019-04-01 11:56:00', '2019-04-01 11:55:00', MINUTE) Output: 1

Specify the granularity of a time

DATETIME_TRUNC(datetime_expression, part) Example: DATETIME_TRUNC('2019-04-01 11:55:00', HOUR) Output: 2019-04-01 11:00:00

This is not by any means an exhaustive guide to date and time expressions in BigQuery. It is only meant as a quick reference to those I use most often. For a more comprehensive guide please see the Google BigQuery documentation.