How to Round Timestamps in BigQuery
Rounding/truncating timestamps, datetimes, etc is helpful when you're grouping by time. In BigQuery, the function you need varies depending on the data type of your schema:
Timestamp
select timestamp_trunc(current_timestamp, HOUR);
Datetime
select datetime_trunc(current_datetime, HOUR);
Date
select date_trunc(current_date, DAY);
Time
select time_trunc(current_time, HOUR);
Depending on the data type, different time units are accepted for each truncating function:
Β | Timestamp | Datetime | Date | Time |
---|---|---|---|---|
microsecond | β | β | Β | β |
millisecond | β | β | Β | β |
second | β | β | Β | β |
minute | β | β | Β | β |
hour | β | β | Β | β |
day | β | β | β | Β |
week | β | β | β | Β |
month | β | β | β | Β |
quarter | β | β | β | Β |
year | β | β | β | Β |