I have some time stamp column which have data like-2022-12-02 10:30:35 AM UTC
I want to remove UTC from this data. This table is in big query
Another approach will be using format_timestamp(). See approach below:
with sample_data as (
select parse_timestamp("%Y-%m-%d %H:%M:%S %p %Z",'2022-12-02 10:30:35 AM UTC') as test_time
)
select
format_timestamp("%Y-%m-%d %H:%M:%S %p",test_time) as new_format
from sample_data
Related
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
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
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
I have a column updated_at that returns an array
["2019-01-05T17:28:32.506-05:00","2019-06-15T13:22:02.625-04:00"]
But I want the output date format like this 2019-01-03.
How can I accomplish this in sql databricks?
Thanks!
Try unnest and cast that as a date:
with ts_array as
(select array['2019-01-05T17:28:32.506-05:00','2019-06-15T13:22:02.625-04:00'] as tsa)
select unnest(tsa)::date from ts_array ;
You can use "date_trunc" SQL function to get the output in date format.
date_trunc(fmt, ts) - Returns timestamp ts truncated to the unit specified by the format model fmt. fmt should be one of [“YEAR”, “YYYY”, “YY”, “MON”, “MONTH”, “MM”, “DAY”, “DD”, “HOUR”, “MINUTE”, “SECOND”, “WEEK”, “QUARTER”]
Examples:
> SELECT date_trunc('YEAR', '2015-03-05T09:32:05.359');
2015-01-01 00:00:00
> SELECT date_trunc('MM', '2015-03-05T09:32:05.359');
2015-03-01 00:00:00
> SELECT date_trunc('DD', '2015-03-05T09:32:05.359');
2015-03-05 00:00:00
> SELECT date_trunc('HOUR', '2015-03-05T09:32:05.359');
2015-03-05 09:00:00
Reference: Databricks - SQL Functions.
Hope this helps.
I am using the following query to change all date to the Monday of the corresponding week:
select date_trunc('week', join_date) as join_wk from my_table
This query converts 2017-08-23 11:30:02 to 2017-08-21 00:00:00
I am wondering if it is possible to remove the hour/min/secondfrom the output 2017-08-21 00:00:00? i.e. make the output in the format of 2017-08-21
date_trunc returns a timestamp. You could cast it to a date to lose the time part of it:
SELECT DATE_TRUNC('week', join_date)::DATE AS join_wk FROM my_table
-- Here ----------------------------^