Date/time types in BigQuery

Introduction to BigQuery

Matt Forrest

Field CTO

Why date and times matter

  1. Easily filter long datasets
  2. Extract parts of dates/times
  3. Partitioning strategies
Introduction to BigQuery

Dates

Example of the DATE data type

SELECT 
    DATE(2010, 05, 19) as launch


/*------------* | launch | +------------+ | 2010-05-19 | *------------*/
Introduction to BigQuery

Timestamps

Example timestamp:

SELECT 
    TIMESTAMP("2010-05-19 00:00:00+00") as launch


/*-------------------------* | launch | +-------------------------+ | 2010-05-19 00:00:00 UTC | *-------------------------*/
Introduction to BigQuery

Datetime and Time

Examples of time and datetime:

SELECT
  TIME(12, 00, 00) as noon,

DATETIME(2019, 05, 19, 00, 00, 00) as launch
/*----------+----------------------* | noon | launch | +----------+----------------------+ | 12:00:00 | 2019-05-19T00:00:00 | *----------+----------------------*/
Introduction to BigQuery

Date and timestamp parts

Day

  • DAY, DAYOFWEEK, DAYOFYEAR.

Week

  • WEEK, WEEKDAY, ISOWEEK.

Month/Year

  • MONTH, QUARTER, YEAR, ISOYEAR.

Time

  • HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND.
1 https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#timestamp_trunc
Introduction to BigQuery

ADD, SUBTRACT, and DIFF

Adding five days

SELECT 
    DATE_ADD(DATE '2010-05-19', 
             INTERVAL 5 DAY) 
             AS five_days_later;

/*--------------------* | five_days_later | +--------------------+ | 2010-05-24 | *--------------------*/

Finding the difference

SELECT 
    DATE_DIFF(DATE '2010-05-24', 
             DATE '2010-05-19', DAY) 
             AS difference;


/*-------------* | difference | +-------------+ | 5 | *-------------*/
Introduction to BigQuery

EXTRACT

Finding the day of the week

SELECT 
    EXTRACT(DAYOFWEEK FROM DATE '2010-05-19') 
             AS day_of_week;


/*--------------* | day_of_week | +--------------+ | 4 | *--------------*/
Introduction to BigQuery

FORMAT

SELECT
    FORMAT_DATE(
      '%x', DATE '2010-05-19') 
    AS with_slashes;


/*--------------* | with_slashes | +--------------+ | 05/19/10 | *--------------*/
SELECT
    FORMAT_DATE(
      '%A', DATE '2010-05-19') 
    AS dow;

/*--------------* | dow | +--------------+ | Wednesday | *--------------*/
1 https://cloud.google.com/bigquery/docs/reference/standard-sql/format-elements#format_elements_date_time
Introduction to BigQuery

Current date/timestamp

Finding the current timestamp:

SELECT
  CURRENT_TIMESTAMP(); 

/*---------------------------------*
 | current_timestamp               |
 +---------------------------------+
 | 2023-11-13 17:35:25.951432 UTC  |
 *---------------------------------*/

Finding the current date:

SELECT
  CURRENT_DATE();

/*---------------*
 | current_date  |
 +---------------+
 | 2023-11-13    |
 *---------------*/
Introduction to BigQuery

Cheat sheet

Data types

  • Dates are exact days, timestamps are absolute dates and times

Date/timestamp parts

  • Parts of a date/timestamp (e.g. MONTH, or HOUR)

Add and subtract

  • Functions to add or subtract date parts (e.g. DATE_ADD)

Difference

  • Difference between two dates by date part (e.g. TIMESTAMP_SUB)

Extract and format

  • Extract a part of a date using date parts, format a date (e.g. EXTRACT and FORMAT)

Current date/timestamp

  • Return the current date or timestamp (e.g. CURRENT_TIMESTAMP)
Introduction to BigQuery

Let's practice!

Introduction to BigQuery

Preparing Video For Download...