Introduction to BigQuery
Matt Forrest
Field CTO
Example of the DATE data type
SELECT DATE(2010, 05, 19) as launch/*------------* | launch | +------------+ | 2010-05-19 | *------------*/
Example timestamp:
SELECT TIMESTAMP("2010-05-19 00:00:00+00") as launch/*-------------------------* | launch | +-------------------------+ | 2010-05-19 00:00:00 UTC | *-------------------------*/
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 | *----------+----------------------*/
Day
DAY, DAYOFWEEK, DAYOFYEAR.Week
WEEK, WEEKDAY, ISOWEEK.Month/Year
MONTH, QUARTER, YEAR, ISOYEAR.Time
HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND.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 | *-------------*/
Finding the day of the week
SELECT EXTRACT(DAYOFWEEK FROM DATE '2010-05-19') AS day_of_week;/*--------------* | day_of_week | +--------------+ | 4 | *--------------*/
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 | *--------------*/
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 |
*---------------*/
Data types
Date/timestamp parts
MONTH, or HOUR)Add and subtract
DATE_ADD)Difference
TIMESTAMP_SUB)Extract and format
EXTRACT and FORMAT)Current date/timestamp
CURRENT_TIMESTAMP)Introduction to BigQuery