'Convert string date format MM/dd/yyyy HH:mm:ss.SSSS to timestamp presto

I have a string that looks like:

2022-03-30 17:18:09.569000

I am trying to convert this to a timestamp as follows:

select "date_parse"("date_format"('2022-03-30 17:18:09.569000', '%Y-%m-%dT%h:%iZ'), '%Y-%m-%dT%h:%iZ')

but i keep getting an error:

Unexpected parameters (varchar(26), varchar(15)) for function date_format. Expected: date_format(timestamp with time zone, varchar(x)) , date_format(timestamp, varchar(x))


Solution 1:[1]

The value in your example matches the format of timestamp literals in SQL, so you can convert it to a timestamp type by just casting the value to the desired type:

> select cast('2022-03-30 17:18:09.569000' as timestamp(6));
           _col0
----------------------------
 2022-03-30 17:18:09.569000
(1 row)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Traverso