Why returns this query 'date_parse(Openingsdatum, '%d-%m-%Y' )' more information then just the day, month and year ?

0

Hi all,

If I run this query:

SELECT date_parse(Openingsdatum, '%d-%m-%Y') Openingsdatum , date_parse(REPLACE(Einddatum, '', NULL), '%d-%m-%Y') Einddatum

I get different errors. First af all, I'm wondering why the output for the openingsdatum is more then the day, month and year. It also included the timestamp: 2022-09-07 00:00:00.000. How can I remove this?

Also, the 'Einddatum' doesn't work either. I found out that I can only convert the string into a dateformat if there are now blank cells. So I tried to do it with the replace function but this doesn't work. I get the error: INVALID_FUNCTION_ARGUMENT: Invalid format: ""

asked 2 years ago244 views
1 Answer
0
Accepted Answer

It is an expected output from date_parse which "parses timestamps"/"returns a timestamp" please follow this presto documentation : https://prestodb.io/docs/current/functions/datetime.html#mysql-date-functions.

You can use this query to truncate the time part from timestamp.

select Date(date_parse('2022-03-01','%Y-%m-%d'))

Also please note that Athena for DML queries uses presto for query execution. So in order to write a DML query upto Athena standards we have to use presto functions only.

Here date_parse() expects a string argument and parses that into timestamp format. Now as you stated that you also have null values. Here I have one question, are these empty values is in the form of "" or null in your table, Because if the empty values are in the form of 'null' then the date_parse should work perfectly fine. Like below:

select Date(date_parse(null,'%Y-%m-%d'))

But this will not :

select Date(date_parse('','%Y-%m-%d'))

because on seeing string '' it performs parsing only to find it is not in the standard format and throws error INVALID_FUNCTION_ARGUMENT: Invalid format: ""

In order to make it work you can use this query

select CASE date_g
           WHEN '' THEN null
           ELSE Date(date_parse(date_g,'%Y-%m-%d'))
       END 
FROM Table_name
AWS
SUPPORT ENGINEER
Shubh
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions