Snowflake - Date string has T character, can't convert to datetime - sql

I have several columns that look like this with a T and +: 2020-04-11T21:00:09+0000
I want to convert them to datetime if possible, I've tried to_timestamp_ntz() and to_date():
to_timestamp_ntz('2020-04-11T21:00:09+0000', 'YYYY-MM-DD HH24:MI:SS.FF+00')
but I keep seeing:
Can't parse '2020-04-11T21:00:09+0000' as timestamp with format...

It is a matter of format:
SELECT to_timestamp_ntz('2020-04-11T21:00:09+0000',
'YYYY-MM-DD"T"HH24:MI:SSTZHTZM') AS res
To handle T it needs to be provided as "T".
Output:
The pattern "..." inside format works for arbitrary text:
SELECT to_timestamp_ntz('2020-04-11aaaa21:00:09+0000',
'YYYY-MM-DD"aaaa"HH24:MI:SSTZHTZM') AS res
-- 2020-04-11 21:00:09.000

I don't think there's a way to specify a date/time format to skip over a character like that. You may have to do something like this:
select to_timestamp_ntz(replace('2020-04-11T21:00:09+0000', 'T', ' '), 'YYYY-MM-DD HH:MI:SSTZHTZM')

Related

Athena Timestamp

What is the appropriate format for my datetime? I've tried several combinations and getting various errors. The data is a string and here is an example: "2022-10-28T00:00:00Z"
Neither of these work:
`WHERE MONTH(parse_datetime(start, 'yyyy-MM-dd"T"HH:mm:ss"Z"')) = 12
`WHERE MONTH(parse_datetime(start, 'yyyy-MM-dd HH:mm:ss')) = 12
You need to use single quotes (') to escape symbol when using Java date functions. To add it to the format string you need to escape it with another one:
select parse_datetime('2023-01-30T20:00:02Z', 'yyyy-MM-dd''T''HH:mm:ss''Z''');
Output:
_col0
2023-01-30 20:00:02.000 UTC
Note that in this case you can just use from_iso8601_timestamp function, which should be more correct approach in general:
select from_iso8601_timestamp('2023-01-30T20:00:02Z');

How to delete "" and change it to date format

My dataset's date column (tweet_stamp) is shown as "2020-01-29 00-21-29" and it is STRING format.
I would like to have result as 2020-01-29.
How to delete "" in string and change it to date format?
I tried code as below
select to_date(from_unixtime(unix_timestamp(tweet_timestamp,'"yyyy-MM-dd 00-00-00"'), 'yyyy-MM-dd')) as tweet_date;
However, result is NULL.
You do not need unix_timestamp+from_unix_time conversion because date part of string is already in right format. Just remove double quotes, get substring and optionally convert to date:
select date(substr(regexp_replace('"2020-01-29 00-21-29"','"',''),1,10)) --returns 2020-01-29
Or even simpler using to_date function:
select to_date(regexp_replace('"2020-01-29 00-21-29"','"','')) --2020-01-29

timestamp conversion for below query

I have these data '2019-12-19T01:39:29.6941632Z' as varchar2 datatype
i have to replace the T and Z and also to convert the datatype to timestamp
I tried this query but won't work
to_timestamp(replace(replace('2019-12-19T01:39:29.6941632Z','T',''),'Z'),'YYYY.MM.DD:HH24:MI:SS')
Please help me
My proposal would be this one:
CAST(TO_TIMESTAMP_TZ('2019-12-19T01:39:29.6941632Z', 'YYYY-MM-DD"T":HH24:MI:SS.FFTZR') AS TIMESTAMP)
As a slight variation on #Wernfried's conversion, since you aren't using the time zone information you can treat the fixed Z as a character literal too, like the fixed T. Then you can use to_timestamp(), and don't need to cast():
TO_TIMESTAMP('2019-12-19T01:39:29.6941632Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF"Z"')
The YYYY, MM, DD, HH24, MI, SS and FF parts of the format mask are as described in the documentation. The "T" and "Z" are character literals so the function knows not to try to interpret them as part of the timestamp value; which means you don't need to remove/replace them. And the overall format mask matches the source string - by default Oracle is forgiving of using the wrong separators (like . instead of -) but it's better to use the correct expected values anyway.
e.g.:
SELECT TO_TIMESTAMP('2019-12-19T01:39:29.6941632Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF"Z"')
FROM DUAL;
19-DEC-19 01.39.29.694163200 AM
(and you can then handle or format the timestamp however you want)
You need to fix the replace(). And use the correct format.
This works:
select to_timestamp(replace(replace('2019-12-19T01:39:29.6941632Z', 'T', ''), 'Z', ''), 'YYYY-MM-DDHH24:MI:SS.FF')
from dual
Here is a db<>fiddle.

Change date format in oracle query

When running
select processing_date from table;
i got this result "04-30-2020 20.12.49.978711"
what i want to change the format of the result to "30-APR-20"
is there a way i can do that ?
i tried select to_date(processing_date,'mm-dd-yyyy') from table; but it gives me errors
any help ?
You want to_char():
select to_char(processing_date, 'MM-DD-YYYY')
Dates are stored as an internal format, which you cannot change. If you want the date formatted in a particular way, then one solution is to convert to a string with the format you want.
EDIT:
The date appears to be a string. You can convert it to a date using:
select to_date(substr(processing_date, 1, 10), 'MM-DD-YYYY')
You can then either use as-is or use to_date() to get the format you really want.

Date type conversion in hive

I have a date column in String type in 'MM/dd/yyyy' format.
I have to convert this into format 'dd/MM/yyyy' format.
How to achieve this in Hive/Impala ?
you can use like this,
select from_unixtime(unix_timestamp(date ,'MM/dd/yyyy'), 'dd/MM/yyyy') from date_test;
Let me know if this works.
Since you only need to switch existing substrings you can use substring:
concat_ws('/',substr(date,1,2),substr(date,4,2),substr(date,7,4))