I am trying to parse and convert date time using below statement. But, its producing wrong result.
SELECT
SAFE.PARSE_TIMESTAMP('%m/%d/%Y %I:%M %p', '9/6/21 7:22 AM',"US/Central")
Also, how to handle daylight savings with bigquery?
BigQuery does handle DST. It might help if you parsed your date in the modern era. I also tend to use city names to be sure I'm getting the local time:
SELECT SAFE.PARSE_TIMESTAMP('%m/%d/%y %I:%M %p', '9/6/21 7:22 AM', "US/Central"),
SAFE.PARSE_TIMESTAMP('%m/%d/%y %I:%M %p', '9/6/21 7:22 AM', 'America/Chicago')
Note that this produces a date this year rather than in the year 21 AD.
Related
Source data has different formats
I am working with a nested JSON table and the created_at field cannot be cast to a timestamp.
Here is in essence what should work:
SELECT
TO_TIMESTAMP(json_data:created_at::VARCHAR, 'mm/dd/yyyy HH:MI:SS AM TZH:TZM') AS custom_date_cast
, TO_TIMESTAMP(json_data:created_at::VARCHAR) AS default_date_cast
FROM my_table
Reason why there is the two fields is because we have dates coming in with this format: 11/21/2022 12:25:30 PM +00:00 (custom_date_cast) and this format 2023-02-07T15:50:09.8385748+00:00 (default_date_cast).
In theory I would create a CASE WHEN to catch the two different cases, alias it to created_at and be done with it. However there are some dates that apparently fit neither of these casts.
What I tried
Here's such an example I retrieved using this query:
SELECT
TRY_TO_TIMESTAMP(json_data:created_at::VARCHAR, 'mm/dd/yyyy HH:MI:SS AM TZH:TZM') AS custom_date_cast
, TRY_TO_TIMESTAMP(json_data:created_at::VARCHAR) AS default_date_cast
, json_data:created_at::VARCHAR AS created_at
FROM my_table
WHERE
TRY_TO_TIMESTAMP(json_data:created_at::VARCHAR, 'mm/dd/yyyy HH:MI:SS AM TZH:TZM') IS NULL
AND TRY_TO_TIMESTAMP(json_data:created_at::VARCHAR) IS NULL
Here's one such result: 4/8/2022 12:49:44 PM +00:00. As you will notice, it is identical to one such example that does work such as: 1/22/2022 4:01:52 PM +00:00.
If I use TO_TIMESTAMP() instead, I get this error for some entries (full breakdown below):
SQL Error [100096] [22007]: Can't parse '4/8/2022 12:49:44 PM +00:00' as timestamp with format 'mm/dd/yyyy HH:MI:SS AM TZH:TZM'
Using this query above I used a COUNT(*) to see how many cases we had with this weird anomaly. The results:
AND
Custom Casting Worked
Custom Casting Failed
Default Casting Worked
0
957
Default Casting Failed
204,588
19,527
This means there are 19,527 entries that cannot be cast either by my custom cast or by the default casting method.
What is even stranger to me is that if I literally copy one of the "non-working" date as an example, then run this SELECT statement it will work just fine:
SELECT
TO_TIMESTAMP('4/8/2022 12:49:44 PM +00:00', 'mm/dd/yyyy HH:MI:SS AM TZH:TZM');
Does anyone have any idea of what could be the problem or what I could try next?
Try creating a Python UDF to parse the dates.
Anaconda already provides in Snowflake "dateutil":
https://github.com/dateutil/dateutil/
It offers what you need:
Generic parsing of dates in almost any string format;
For example:
from dateutil.parser import *
now = parse("Sat Oct 11 17:13:46 UTC 2003")
Docs:
https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.parse
I have a column filled with dates in string format, e.g. 2023-01-31 11:21:33 GMT.
I am trying to write a query that will select a year and a month and will do some calculations later on. My standard approaches using EXTRACT(YEAR FROM a)) etc. did not work. Therefore, I am trying to parse datetime using PARSE_DATETIME(a, 'yyyy-mm-dd hh:mm:ss'). The thing is, I don't know how to format "GMT" and google did not help with that.
The error message is INVALID_FUNCTION_ARGUMENT: Invalid format: "2023-01-31 11:21:33 GMT" is malformed at "GMT".
Use 'yyyy-MM-dd HH:mm:ss z':
select parse_datetime('2023-01-31 11:21:33 GMT', 'yyyy-MM-dd HH:mm:ss z')
Output:
_col0
2023-01-31 11:21:33.000 UTC
parse_datetime is Java date function which uses JodaTime’s DateTimeFormat pattern format which is mostly compatible with java.text.SimpleDateFormat with z matching general timezone.
I've tried multiple solutions, but I keep getting errors. I need to create a new column casting VARCHAR to TIMESTAMP that includes AM, PM or -ideally- changes it to 24 hrs format.
VARCHAR format (Start_Date column): 8/3/2022 4:58:49 PM
I found the below solution is some other post, but I'm getting error: 'Format code appears twice'
SELECT itab.*,
TO_TIMESTAMP(Start_Date, 'MM/DD/YYYY HH:MM:SS AM') AS start_TS
FROM db.info_table itab
Please advise.
You have two problems.
MI is the format for minutes, MM is for months (you have it twice, this is why you are getting that error).
Your date/time string has single digit values for month, day, etc. You can use a pretty simple regex for that.
select to_timestamp(regexp_replace('8/3/2022 4:58:49 PM', '\b([0-9])\b', '0\1'), 'MM/DD/YYYY HH:mi:SS AM')
TO_TIMESTAMP returns a TIMESTAMP(6). If you don't want microseconds you can specify the precision using
CAST(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS timestamp(0) FORMAT 'MM/DD/YYYYbHH:Mi:SSbT')
All you need is pad day and month in Teradata (as opposed to Oracle etc). m/d/y format has not been implemented.
select '8/3/2022 4:58:49 PM' date1,
to_timestamp(
lpad(strtok(date1,'/',1),2,'0')||'/'||lpad(strtok(date1,'/',2),2,'0')||'/'||strtok(date1,'/',3)
,'mm/dd/yyyy hh24:mi:ss AM'
);
My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.
This question already has answers here:
Convert timestamp to date in Oracle SQL
(9 answers)
Closed 2 years ago.
i have to convert different timezone string to date. in my table, i have column defined as varchar2. which contains data as below. i need to convert to one time zone(exampple to central time) and extract date from that. i tried different ways but getting invalid month error or not date. ANy suggestions from your end?
1/16/2020 6:28:44 AM -08:00
11/8/2019 3:20:30 AM -05:00
10/25/2019 6:08:21 PM +05:30
I believe you first need to convert it to timestamp with timezone ad then cast it to date:
select CAST(to_timestamp_tz (dat_col, 'MM/DD/YYYY HH12:MI:SS AM TZH:TZM' ) AS DATE)
from test
here is a demo
This will turn your data into specific timezone :
select
to_timestamp_tz(dat_col, 'MM/DD/YYYY HH12:MI:SS AM TZH:TZM')at time zone 'Europe/Moscow'
from test
Then you can cast that :
select
CAST
(to_timestamp_tz(dat_col, 'MM/DD/YYYY HH12:MI:SS AM TZH:TZM')at time zone 'Europe/Moscow'
AS DATE)
from test
Please provide your expected results for more accurate code...