1

Bigquery suggest me 'EXTRACT' funtion syntax:

extract(datetime_part FROM timestamp_expr [AT TIME ZONE tz_spec])

However, Bigquery notice "Unexpected keyword AT". Moreover, it's syntax in official Bigquery document is totally different. Similar case with 'Date' function. Can anyone explain this? What should i fill in syntax is suggested by BQ? Does have any similar cases? Thanks you!!

An clear explaination.

3
  • Maybe you have just misunderstood the syntax description. The brackets must not be typed. They just mean that the part AT TIME ZONE tz_spec is optional. Commented Sep 18, 2024 at 6:21
  • I changed the title to a more descriptive one. Imagine someone else having problems with the EXTRACT syntax... Would they look for a topic titled "weird syntax"? Probably not. Commented Sep 18, 2024 at 6:43
  • @ThorstenKettner thanks for your comment, It is usefull. I will mind for future title. Commented Sep 18, 2024 at 12:43

1 Answer 1

0

The allowed syntax depends on whether the operand is a DATE, a DATETIME or a TIMESTAMP.

EXTRACT function

For a TIMESTAMP:

EXTRACT(part FROM timestamp_expression [AT TIME ZONE time_zone])

https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#extract

For a DATE or DATETIME:

EXTRACT(part FROM date_expression)

https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#extract

DATE function

For a TIMESTAMP:

DATE(timestamp_expression [, time_zone_expression])

https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date

(It's a bit confusing that they have this under "date functions" and not "timestamp functions", because while the result is a date, the function is applied on a timestamp and should hence be put in that chapter.)

For a DATETIME:

DATE(datetime_expression)

https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#date

Sign up to request clarification or add additional context in comments.

1 Comment

Not being a BigQuery developer, I was a bit surprised at these syntaxes, too. I would have expected AT TIME ZONE to be a TIMESTAMP operator which you can apply to a timestamp in any situation (and hence thought I could look this up in cloud.google.com/bigquery/docs/reference/standard-sql/operators). Obviously, this isn't the case. AT TIME ZONE is not an operator unfortunately, but part of the respective function syntax, and with EXTRACT you can use this syntax, while with DATE you must state the time zone as a parameter instead. A bit of a weakness in design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.