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

11,141

Solution 1

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
);

Solution 2

I was looking for the most resent visitStartTime in the ga_realtime_sessions table in BiqQuery. I parsed the timestamps as follows:

SELECT
  visitStartTime, 
  TIMESTAMP_SECONDS(visitStartTime) as starttime_UTC,
  DATETIME(TIMESTAMP_SECONDS(visitStartTime), "Europe/Amsterdam") as starttime_LOCAL,
FROM `ga_sessions_*`
ORDER BY visitStartTime desc 
LIMIT 30

Resulting in records like:

Row visitStartTime  starttime_UTC             starttime_LOCAL   
1   1562153975      2019-07-03 11:39:35 UTC   2019-07-03T13:39:35
Share:
11,141
Thomas Chamberlain
Author by

Thomas Chamberlain

Updated on June 27, 2022

Comments