BQ snapshot - set timestamp from the past - sql

I'm trying to create a snapshot of a table using SQL. I want to capture the data as of 8am UTC today. Wrote the following code but it returns errors, probably the issue is in the last line but I can't figure out what the syntax should be. Thanks in advance.
CREATE SNAPSHOT TABLE `project.dataset.table_snapshot`
CLONE `project.dataset.table`
OPTIONS(
expiration_timestamp = TIMESTAMP "2023-03-10 00:08:00 UTC",
timestamp = "2023-02-10 00:08:00 UTC"
)

Your timestamp is in the future, it is in March.
Timetravel is supported in SELECT statements only.
#CREATE TABLE `project.dataset.table_snapshot`
# OPTIONS( expiration_timestamp = TIMESTAMP "2023-03-10 00:08:00 UTC" )
SELECT *
FROM `mydataset.mytable`
FOR SYSTEM_TIME AS OF "2023-02-10 00:08:00 UTC"

Related

Group timestamp information by date and hour in Bigquery

I have this timestamp metric which shows the following information:
2021-08-30 22:10:22.838 UTC
I would like to split and group this info by date and hour, so it should look something like this in BQ:
Date: 2021-08-30
Hour: 22:00:00 UTC
Anyone know how do do this?
Thanks!!
To extract the date and hour you can use this (replacing your_ts with the appropriate field name).
SELECT
EXTRACT(DATE FROM your_ts) dt,
EXTRACT(HOUR FROM your_ts) hr
FROM tbl
If you want to keep the formatting you provided (returning strings), you can try something like this.
SELECT
FORMAT_TIMESTAMP("%F", your_ts) dt,
FORMAT_TIMESTAMP("%X", TIMESTAMP_TRUNC(your_ts, HOUR))
FROM tbl

Extract time on sql gcp

I am extracting the time from timestamp using sql on gcp. The timestamp is
2020-05-10 06:14:25.276 UTC
My code is
SELECT
cast( Request_Timestamp as time) AS Time,
FROM conversation;
However, I got the result:
18:08:47.371000
What should I code so I will get the result like:
18:08:47
Thanks
Below is for BigQuery Standard SQL
#standardSQL
SELECT TIME_TRUNC(TIME(Request_Timestamp), SECOND) AS Time
FROM `project.dataset.conversation`
So when applied to 2020-05-10 06:14:25.276 UTC timestamp - output it
Row Time
1 06:14:25

How to convert UTC time to local timezones based on timezone column in bigquery?

I've been trying to convert each UTC time back to the appropriate local timezone using standard SQL in GBQ, but couldn't find a good way to do it dynamically because I might have tons of different timezone name within the database. I'm wondering if anyone has an idea?
The table I have contains 2 different columns (see screenshot)
Below example is for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.yourtable` AS (
SELECT 'Pacific/Honolulu' timezone, TIMESTAMP '2020-03-01 03:41:27 UTC' UTC_timestamp UNION ALL
SELECT 'America/Los_Angeles', '2020-03-01 03:41:27 UTC'
)
SELECT *,
DATETIME(UTC_timestamp, timezone) AS local_time
FROM `project.dataset.yourtable`
with output
Row timezone UTC_timestamp local_time
1 Pacific/Honolulu 2020-03-01 03:41:27 UTC 2020-02-29T17:41:27
2 America/Los_Angeles 2020-03-01 03:41:27 UTC 2020-02-29T19:41:27

Apache Hive- Time Stamp query

I have two time stamp columns in a Hive DB storing timestamp in following format:
hive> select last_date from xyz limit 2;
OK
2019-08-21 15:11:23.553
2019-08-21 15:11:23.553
[Above has milliseconds stored in it by default]
hive> select last_modify_date from xyz limit 2;
OK
2018-04-18 23:32:58
2017-09-22 04:02:32
I need a common Hive select query which would convert both the above timestamps to 'YYYY-MM-DD HH:mm:ss.SSS' formats, preserving the millisecond value if exists, or appending '.000' if it doesnt exist.
What I have tried so far:
select
last_modify_date,
from_unixtime(unix_timestamp(last_modify_date), "yyyy-MM-dd HH:mm:ss.SSS") as ts
from xyz limit 3;
However, the above query displays '.000' for both the above said timestamp columns.
Please help
From the UDF that implements unix_timestamp, you can see that the returned value is in SENCONDS represented by a LongWritable. And anything less than one second is rounded off.
You can write your own UDF, or just use pure SQL to achieve that.
One of the easy way is to use the GenericUDFRpad rpad:
select rpad(your_date, 23, '.000') from your_table;
Some examples:
hive> select rpad('2018-04-18 23:32:58', 23, '.000');
OK
2018-04-18 23:32:58.000
hive> select rpad('2018-04-18 23:32:58.553', 23, '.000');
OK
2018-04-18 23:32:58.553

Create a view in bigquery with date rounded down to midnight?

I am creating a view in BigQuery with the following SQL statement:
SELECT Id, CreatedDate FROM [bucketname.tablename]
How can I modify this query so that the CreatedDate becomes rounded down to midnight in the view?
in BigQuery legacy SQL: try TIMESTAMP(DATE(CreatedDate))
as an example
CreatedDate = '2016-04-30 07:01:28 UTC'
DATE(CreatedDate) = '2016-04-30'
and finally TIMESTAMP(DATE(CreatedDate)) = '2016-04-30 00:00:00 UTC'
In mode of standart SQL: try Format functions
FORMAT_TIMESTAMP("%Y-%m-%d 00:00:00", CURRENT_TIMESTAMP())
I believe you can just use the DATE function:
Returns a human-readable string of a TIMESTAMP data type in the format %Y-%m-%d.
So:
SELECT Id, DATE(CreatedDate) AS CreatedDate From [dataset.tablename]