问题
A quick question to Bigquery gurus.
Here are two methods for extracting date from a timestamp in Bigquery using standardSQL
#standardSQL
#1
DATE(TIMESTAMP_MILLIS(CAST((timestamp) AS INT64)))
#2
EXTRACT(DATE FROM TIMESTAMP_MILLIS(timestamp))
Which one is more preferable and why? Thanks!
回答1:
It really comes down to personal preference; one isn't superior to the other since they have the same semantics and performance. The argument in favor of using EXTRACT
is that if you are extracting other date/time parts in the select list, it mirrors them. For example:
SELECT
EXTRACT(DATE FROM TIMESTAMP_MILLIS(timestamp)) AS date,
EXTRACT(ISOYEAR FROM TIMESTAMP_MILLIS(timestamp)) AS iso_year,
EXTRACT(ISOWEEK FROM TIMESTAMP_MILLIS(timestamp)) AS iso_week
FROM YourTable;
Compared to:
SELECT
DATE(TIMESTAMP_MILLIS(timestamp)) AS date,
EXTRACT(ISOYEAR FROM TIMESTAMP_MILLIS(timestamp)) AS iso_year,
EXTRACT(ISOWEEK FROM TIMESTAMP_MILLIS(timestamp)) AS iso_week
FROM YourTable;
来源:https://stackoverflow.com/questions/45933531/extracting-date-from-timestamp-in-bigquery-a-preferable-method