Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery compare Date

In BigQuery table what I using some date stored in DateTime typed columns with value like "2016-01-20T00:00:00". I want to run query that for example show row which included in some range (lets say from 2016-01-01 to 2016-02-28).

So problem is that when I put

...Where data < TIMESTAMP('2017-01-30 00:00:00')...//less then

it show me nothing but when I change it to opposite value like

...Where data > TIMESTAMP('2017-01-30 00:00:00')...//greater then

it returns me even those values that logically should be excluded (like 2017-01-20)

I have tested that TIMESTAMP('2017-01-20 00:00:00') returning '2017-01-26 00:00:00 UTC'.

So which method from bigQuery DateTime I should use to be able to compare dates that I have? May be I need to convert both of them into some kind 'milliseconds since' value?

Thanks

like image 412
maximelian1986 Avatar asked Jul 03 '17 12:07

maximelian1986


People also ask

What query would you use to find each user between two dates in GCP BigQuery?

Try: Date(date_column) between current_date()...

How do you subtract dates in BigQuery?

Subtracting a unit 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.


2 Answers

Thank you all for help. Your answers and link has leaded me to solution.

where data<cast('2017-01-23 00:00:00' as datetime)

cast('2017-01-23 00:00:00' as datetime) provide '2017-01-23T00:00:00' value and bigQuery are happy with it and give me wanted result.

like image 164
maximelian1986 Avatar answered Oct 19 '22 16:10

maximelian1986


If your column is of datetime int:

Where data < 20170130

and

Where data > 20170130

If your column is of datetime datatype, drop the TIMESTAMP:

Where data < '2017-01-30 00:00:00'

and

Where data > '2017-01-30 00:00:00'
like image 2
cloudsafe Avatar answered Oct 19 '22 18:10

cloudsafe