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: ""

已提問 2 年前檢視次數 251 次
1 個回答
0
已接受的答案

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
支援工程師
Shubh
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南