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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠