Parse string to TIMESTAMP BigQuery - google-bigquery

I have a date stored as a string in this format:
Fri Jul 01 2022 00:00:00 GMT+0000 (Coordinated Universal Time)
How can one convert it to TIMESTAMP in Google BigQuery?

You may try and consider below approach:
select PARSE_TIMESTAMP('%a %b %d %Y %T %z', REGEXP_EXTRACT(
REGEXP_REPLACE('Fri Jul 01 2022 00:00:00 GMT+0000 (Coordinated Universal Time)', r'GMT', ''),
r'(^\w{3}\s\w{3}\s\d{1,2}\s\w{4}\s\d{2}\:\d{2}\:\d{2}\s\+\d{4}).+')) as timestamp_bq
Output:

Related

convert Thu Sep 02 16:29:11 UTC 2021 to timestamp in snowflake

How to convert varchar Thu Sep 02 16:29:11 UTC 2021 to the timestamp in the snowflake database?
SELECT TO_TIMESTAMP_NTZ('Thu Sep 02 16:29:11 UTC 2021', 'DY MON DD HH24:MI:SS UTC YYYY');
For reference, here is a link to the documentation on the "parts".
https://docs.snowflake.com/en/sql-reference/functions-conversion.html

how to convert long verbal datetime to timestamp (YY-MM-DD HH:MM:SS) in snowflake?

I'm using snowflake dates.
I have date in weird pattern (output from database):
Wed Apr 21 2021 22:11:32 GMT+0300 (Israel Daylight Time)
I need to parse it as datetime- YY-MM-DD HH:MM:SS.
if I try this out:
SELECT TO_TIMESTAMP_NTZ('Wed Apr 21 2021 22:11:32 GMT+0300 (Israel Daylight Time)', 'YY:MM:DD
HH:MM:SS')
I get this error:
SQL Error [100096] [22007]: Can't parse 'Wed Apr 21 2021 22:11:32 GMT+0300 (Israel Daylight Time)' as
timestamp with format 'YY:MM:DD HH:MM:SS'
and so on in every function I tried!!
(TO_TIMESTAMP_NTZ, TO_TIMESTAMP_LTZ, TO_TIMESTAMP_TZ, TO_TIMESTAMP, TO_DATETIME, TO_DATE, TO_TIME).
any idea?
Using the values at Timestamp Formats, and trimming the string down we can get the following working
SELECT TO_TIMESTAMP_NTZ('Wed Apr 21 2021 22:11:32', 'DY MON DD YYYY HH:MM:SS');
adding the timezone back in with
SELECT TO_TIMESTAMP_NTZ('Wed Apr 21 2021 22:11:32 GMT+0300', 'DY MON DD YYYY HH:MM:SS GMTTZHTZM');
this works, but gives a NoTimeZone value, when the value has a timezone, so purhaps NTZ is not what you wanted.
But the (Israel Daylight Time) part is throwing us for a loop, so lets get rid of that with a REGEX_SUBSTR
SELECT 'Wed Apr 21 2021 22:11:32 GMT+0300 (Israel Daylight Time)' as in_str
,REGEXP_SUBSTR( in_str , '(.*) \\(',1,1,'c',1) as regex_str
,TO_TIMESTAMP_NTZ(regex_str, 'DY MON DD YYYY HH:MM:SS GMTTZHTZM') as time
;
gives:
IN_STR
Wed Apr 21 2021 22:11:32 GMT+0300 (Israel Daylight Time)
REGEX_STR
Wed Apr 21 2021 22:11:32 GMT+0300
TIME
2021-11-21 22:00:32.000

Convert a day, date and timestamp string in the format YYYY-MM-DD HH:MM:SS in SQL

Is there any way to convert "A day, date and timestamp string in the format YYYY-MM-DD HH:MM:SS” using standard SQL syntax in BigQuery.
For eg.:
Fri, 31 Aug 2018 13:00:57 +0000
Here is what I tried to come up with:
SELECT PARSE_DATETIME('%a,%d %b %Y %E#S' , 'Fri, 31 Aug 2018 13:00:57 +0000')
Below example for BigQuery Standard SQL
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'Fri, 31 Aug 2018 13:00:57 +0000' ts_string
)
SELECT
ts_string,
PARSE_TIMESTAMP('%a, %d %b %Y %X %z' , ts_string) ts_timestamp,
DATETIME(PARSE_TIMESTAMP('%a, %d %b %Y %X %z' , ts_string)) ts_datetime
FROM `project.dataset.table`
with result
Row ts_string ts_timestamp ts_datetime
1 Fri, 31 Aug 2018 13:00:57 +0000 2018-08-31 13:00:57 UTC 2018-08-31T13:00:57

PostgreSQL: Convert MongoDB date format to timestamptz

I have referred to the psql documentation and came up with this query.
SELECT to_timestamp('Tue Aug 30 2016 04:07:13 GMT+0530 (IST)', 'Dy MON DD YYYY HH24:MI:SS');
This date time string Tue Aug 30 2016 04:07:13 GMT+0530 (IST) is what I got from MongoDB printjson(createdAt).
The above postresql doesn't seem to work correctly for all offsets.
I tried this
select to_timestamp('Tue Aug 30 2016 04:07:13 GMT+0530 (IST)', 'Dy MON DD YYYY HH24:MI:SS "GMT"OF "(IST)"');
But I get this error ``ERROR: "TZ"/"tz"/"OF" format patterns are not supported in to_date`.
How to convert to psql timestamptz format from this string Tue Aug 30 2016 04:07:13 GMT+0530 (IST) ?
It looks an ugly wheel and requires such replace for each unrecognized offset, but it works. For your sample replace 'GMT+0530 (IST)' to 'GMT+05:30' and it will be picked up:
t=# select replace('Tue Aug 30 2016 04:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz;
replace
------------------------
2016-08-30 09:37:13+00
(1 row)
t=# select replace('Tue Aug 30 2016 14:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz;
replace
------------------------
2016-08-30 19:37:13+00
(1 row)
update: depending on your timezone result can be confusing:
t=# set timezone TO 'GMT-5:30'; SET
t=# select replace('Tue Aug 30 2016 14:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz;
replace
---------------------------
2016-08-31 01:07:13+05:30
(1 row)
to check if it is right, use:
t=# select replace('Tue Aug 30 2016 14:07:13 GMT+0530 (IST)','GMT+0530 (IST)','GMT+05:30')::timestamptz at time zone 'UTC';
timezone
---------------------
2016-08-30 19:37:13
(1 row)

How to get date-format with day + month name and GMT?

I want to write the current date in the following format in VB.net:
Tue, 27 Jul 2010 00:00:00 GMT+02:00
Any help would be appreciated.
Perhaps simply:
Date.Now.ToString("R") ' Fri, 14 Nov 2014 12:32:18 GMT
The RFC1123 ("R", "r") Format Specifier