PrestoSQL parse_datetime with suffix +XX - sql

In PrestoSQL, I'm trying to use parse_datetime to convert a field to timestamp.
The field is in the format of "2014-01-01 00:00:00+07", I tried using the following but its throwing an error, and I could not find any doc on this format:
parse_datetime(row.created_at,'YYYY-MM-dd HH:mm:ss.SSSSSS+ZZ')
Error I'm seeing is
Invalid format: "2014-01-01 00:00:00+00" is malformed at "+00"
What would be the correct way to parse this format of datetime?

You can try YYYY-MM-dd HH:mm:ssZ format:
trino> select parse_datetime('2014-01-01 00:00:00+00','YYYY-MM-dd HH:mm:ssZ');
_col0
-----------------------------
2014-01-01 00:00:00.000 UTC

Related

Date format in BigQuery

Edited: Want to convert the format.
I am kinda new in BigQuery, recently I was working on a project. I want to convert above type of format into yyyy/mm/dd format. How will I do that?
You can combine PARSE_DATE with FORMAT_DATE to get the desired output:
SELECT FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y","June 10, 2014")) AS date_str
PARSE_DATE will parse the provided string into a DATE type value in BQ format (YYYY-MM-DD) and then FORMAT_DATE will format this DATE to a string with the desired output.
In case someone is wondering how to convert the whole column;
FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y",ColumnName)) as DesiredColumnName

Bigquery cannot parse date from datetime

Bigquery cannot parse datetime from a date in spite of providing right format. Dates in my column are of the same format I used in the string:
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%z',"2021-03-22T14:00:00-03:00"))
This gives this error:
Failed to parse input string "2021-03-22T14:00:00-03:00"
I am trying to make this work by this answer
You should use %Ez instead of %z as in below
SELECT EXTRACT(DATE FROM PARSE_TIMESTAMP('%Y-%m-%dT%H:%M:%S%Ez',"2021-03-22T14:00:00-03:00"))
See Supported format elements for TIMESTAMP for more details

Trouble converting malformed ISO8601-formatted varchar to timestamp

I was trying to convert a timestamp string with the following format into a timestamp format but encountered an error as such:
Invalid format: "20201216T090000+0000" is malformed at "0000+0000"
Original Query
SELECT from_iso8601_timestamp(date_ts)
FROM
...
I thought of applying substr to retrieve 20121216T09 only as a string, which I know would work with from_iso8601_timestamp. But any other advice would be appreciated!
The from_iso8601_timestamp() supports format with dashes and colons:
presto> SELECT from_iso8601_timestamp('2020-12-16T09:00:00+00:00');
_col0
-----------------------------
2020-12-16 09:00:00.000 UTC
In the case input is formatted like 20201216T090000+0000, the parse_datetime() would be more appropriate.

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs

Hive - UNIX_TIMESTAMP Quandary

Here is my 1 line of data (for brevity):
73831 12/26/2014 1:00:00 AM 0.3220
The 2nd column is the time column which is in string format. I'm using this hive query:
select col2, UNIX_TIMESTAMP(col2,'MM/DD/YYYY hh:mm:ss aaa') from Table
Here is what I get: 1388296800
However, when I check with, http://www.epochconverter.com/ and also from_unixtime(1388296800), I get a different date.
Is there something wrong with my format / pattern string I enter into UNIX_TIMESTAMP in Hive?
Your date format symbols need to conform to those in the Java SimpleDateFormat documentation.
For your date it looks like you want MM/dd/yyyy HH:mm:ss aa.