How to use DATE_TRUNC function in Bigquery ?

Date_Trunc function can be used to convert to a date to the nearest week or month or quarter or year. It can be highly useful while computing monthly aggregates.

DATE_TRUNC Usage examples:

SELECT DATE_TRUNC(DATE '2020-12-25', MONTH) as month;
2020-12-01
SELECT DATE_TRUNC(DATE '2020-12-25', YEAR) as YEAR;
2020-01-01
SELECT DATE_TRUNC(DATE '2020-12-25', QUARTER) as QUARTER;
2020-10-01
SELECT DATE_TRUNC(DATE '2020-12-25', WEEK) as WEEK;
2020-12-20
SELECT DATE_TRUNC(DATE '2020-12-25', DAY) as DAY;
2020-12-25

mCollins

posted on 17 Apr 19

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds