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
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.
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.
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
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With