Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert visit start time to a readable human date or timestamp in Big Query?

I'm trying to convert the visit start time variable which is captured in big query to a readable date or preferably timestamp.

I'm using standard SQL.

I've tried using sec_to_timestamp but I believe this only works in legacy SQL within BQ.

The google documentation talks about converting to timestamp from date expression and string expression however the visit start time is an integer.

https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators

I've looked at the following link and tried the code in this but I get the error "function not found: integer"

Big Query - Google Analytics - Time diff between first visit and purchase

Any other advice on dates/times captured by BQ would be greatly received.

Thank you

like image 398
Thomas Chamberlain Avatar asked Feb 28 '17 17:02

Thomas Chamberlain


People also ask

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.

What is the timestamp format 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.


1 Answers

Assuming that your start time is in seconds relative to the Unix epoch, you can use TIMESTAMP_SECONDS to convert it to a timestamp. For example:

#standardSQL
SELECT
  TIMESTAMP_SECONDS(start_time_sec) AS timestamp
FROM (
  SELECT 1488303123 AS start_time_sec
);

If you want to convert the timestamp to a date, you can use EXTRACT with an optional timezone:

#standardSQL
SELECT
  EXTRACT(DATE FROM TIMESTAMP_SECONDS(start_time_sec)
          AT TIME ZONE 'America/Los_Angeles') AS date
FROM (
  SELECT 1488303123 AS start_time_sec
);
like image 104
Elliott Brossard Avatar answered Sep 22 '22 11:09

Elliott Brossard