Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DATE to TIMESTAMP on BigQuery/StandardSQL

I am querying a table on BigQuery that has a field in the 'DATE' format. I want to read this in the 'TIMESTAMP' format. I tried converting the DATE to an integer and then converting into a TIMESTAMP but doesn't seem to work.

like image 720
Nivi Avatar asked Jun 13 '18 13:06

Nivi


People also ask

How do you create a timestamp in BigQuery?

The format is: YYYY-MM-DD HH:MM:SS (e.g. 2021-05-15 16:45:23). Timestamp type: Date, time, and time zone information are all included in timestamps. If no time zone is given, the format falls back to UTC. The format is: YYYY-MM-DD [Timezone] HH:MM:SS (e.g. 2021-05-15 16:45:18 UTC).

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.


1 Answers

#standardSQL
WITH `project.dataset.table` AS (
  SELECT CURRENT_DATE() AS dt
)
SELECT dt, CAST(dt AS TIMESTAMP) AS ts,
  TIMESTAMP(dt) AS ts2
FROM `project.dataset.table`   

with result as

Row dt          ts                           ts2
1   2018-06-13  2018-06-13 00:00:00.000 UTC  2018-06-13 00:00:00.000 UTC       
like image 84
Mikhail Berlyant Avatar answered Sep 21 '22 11:09

Mikhail Berlyant