Round to nearest MINUTE or HOUR in BigQuery

How to round to nearest MINUTE or Hour in Google Bigquery

\n\n

BigQuery Standard SQL Syntax:

\n\n \n\n
#standardSQL\nWITH `project.dataset.table` AS (\n  SELECT DATETIME '2018-01-01 01:05:56' input_datetime \n)\nSELECT input_datetime,\n  DATETIME_TRUNC(input_datetime, MINUTE) rounded_to_minute,\n  DATETIME_TRUNC(input_datetime, HOUR) rounded_to_hour\nFROM `project.dataset.table`   \n
\n\n

Result:

\n\n
Row     input_datetime          rounded_to_minute       rounded_to_hour  \n1       2018-01-01T01:05:56     2018-01-01T01:05:00     2018-01-01T01:00:00  \n
\n\n

For TIMESTAMP or TIME data types - you can use respectively - TIMESTAMP_TRUNC() or TIME_TRUNC()

Atori

posted on

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