Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery - how to compare a "date" column (using legacy SQL)?

I have a BigQuery table with a column Date which is a date type. I am trying to run this query:

SELECT * FROM dataset.table_name WHERE Date = "2016-07-11"

This throws the error:

Argument type mismatch in function EQUAL: 'Date' is type int32, '2016-07-11' is type string

I have also tried this query:

SELECT * FROM dataset.table_name WHERE Date = TIMESTAMP("2016-07-11")

but this returns 0 results, although my table contains at least one record with this value (2016-07-11) in the Date column.

So, how can I compare a date field in BigQuery?

like image 877
rereradu Avatar asked Aug 22 '16 09:08

rereradu


People also ask

What is legacy SQL in BigQuery?

Previously, BigQuery executed queries using a non-standard SQL dialect known as BigQuery SQL. With the launch of BigQuery 2.0, BigQuery released support for standard SQL, and renamed BigQuery SQL to legacy SQL. Standard SQL is the preferred SQL dialect for querying data stored in BigQuery.

What is the difference between datetime and timestamp in BigQuery?

Datetime type: comprises both calendar date and time. It does not store time zone information: YYYY-MM-DD HH:MM:SS (e.g. ). Timestamp type: comprises date, time, and time zone information.

How do you query a nested column in BigQuery?

BigQuery automatically flattens nested fields when querying. To query a column with nested data, each field must be identified in the context of the column that contains it. For example: customer.id refers to the id field in the customer column.

How do you subtract dates in BigQuery?

Subtracting a specific amount of days, weeks, months, quarters, or years from a date can be done using the DATE_SUB function. The first argument takes a date and the second argument takes an interval, a numeric value, and a unit. The supported units (DATE_PART) are: Days: DAY.


2 Answers

Try below

WHERE DATE(Date) = "2016-07-11"

My additional recommendation would be to not to use reserved words as column's name, I think if your column was named properly - your original WHERE clause would worked perfectly and you would not need to use workaround with DATE()=""

like image 83
Mikhail Berlyant Avatar answered Oct 19 '22 09:10

Mikhail Berlyant


This solution wasn't working for me:

DATE(Date) = "2016-07-11"

Instead, I had to use:

Date = TIMESTAMP("2016-07-11")
like image 2
Ramiro Agustin Palacios Avatar answered Oct 19 '22 09:10

Ramiro Agustin Palacios