Converting timestamp string to timestamp in Presto - Malform error - sql

I am trying to convert a timestamp string to timestamp with date_parse, but keep getting an error. Any suggestions? I am working on Presto SQL. I have tried to replace %H:%f with %T and it still doesn't work.
date_parse(sg."#timestamp", '%Y-%m-%d %H:%f')
The error message is:
presto: query failed (200 OK): "USER_ERROR: com.facebook.presto.spi.PrestoException: Invalid format: "2017-12-31 08:29:02.12" is malformed at ":02.12""
Any help would be greatly appreciated, thanks!

You need date_parse() with this format:
presto:default> SELECT date_parse('2017-12-31 08:29:02.12', '%Y-%m-%d %H:%i:%S.%f');
_col0
-------------------------
2017-12-31 08:29:02.120
(1 row)

Related

Can not parse the date field

I have been trying to fix this issue but could not find a solution to it.
I have a date column as string type . Trying to convert it to timestamp using date_parse but getting the following error.
INVALID_FUNCTION_ARGUMENT: Invalid format: "2021-09-28
21:05:28.272159" is malformed at "21:05:28.272159"
my query is
"date_parse"(a.commitdatetime, '%y-%m-%d %h:%i:%s.%f') commitdatetime
do you have any ideas how to fix this issue? Thanks for your time from now
Please refer to format strings documentation. %y and %h should be substituted with %Y and %H correspondingly:
select date_parse('2021-09-28 21:05:28.272159', '%Y-%m-%d %H:%i:%s.%f');
Output:
_col0
2021-09-28 21:05:28.272

STRING - DATE FORMAT BIGQUERY

error is : Invalid timestamp: '2/17/2022'
Hi guys, Someone can help me to change STING (dd-mm-yyy) to a Date fomat (yyyy-mm-dd)?
thank you for your time
Try below
select parse_date('%m/%d/%Y', '2/17/2022'), parse_date('%m-%d-%Y', '2-17-2022')
with output

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.

Converting string to timestamp issues sql

I'm trying to cast a string to timestamp but I'm getting the following error:
Failed to output to file. Query failed: Value cannot be cast to timestamp: 2020-03-23T05:17:44.000Z
I'm using the query below:
select CAST(purchase_date AS timestamp)
from main_table
You can use from_iso8601_timestamp function if timestamp with time zone type is acceptable.
Or, you can use date_parse function.
Try parse_datetime():
select parse_datetime('2020-03-23T05:17:44.000Z', '%Y-%m-%dT%H:%i:%s.%fZ')

Failed to parse input string in BigQuery with parse_date

I have a column 'appointment_date' in string formate representing a date dd.mm.yyyy.
In BiqQuery I am using the following query to find all appointments dates lying in the future:
SELECT appointment_date
FROM `appointments`
where parse_date('%d.%m.%Y', appointment_date) > current_date()
BiqQuery returns the following error message: Failed to parse input string ""
Please advice.
Thanks,
Janine
Use safe.parse() to avoid the error:
where safe.parse_date('%d.%m.%Y', appointment_date) > current_date()
This will return NULL for invalid formats rather than an error.