Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Athena date format unable to convert string to date formate

tried the below syntax none of them helped to convert a string type column to date

select INVC_,APIDT,APDDT from APAPP100 limit 10
select current_date, APIDT,APDDT from APAPP100 limit 10
select date_format( b.APIDT, '%Y-%m-%d') from APAPP100 b
select CAST( b.APIDT AS date) from APAPP100 b
select date(b.APIDT) from APAPP100 b
select convert(datetime, b.APIDT) from APAPP100 b
select date_parse(b.APIDT, '%Y-%m-%d') from APAPP100 b
select str_to_date(b.APIDT) from APAPP100 b
like image 716
vinsent paramanantham Avatar asked Sep 14 '17 10:09

vinsent paramanantham


People also ask

How do I get the timestamp from a Presto date?

You can convert timestamp to date with cast(col as date) or date(col) .

What data types cannot be cast to string in Athena?

Non-string data types cannot be cast to STRING in Athena; cast them to VARCHAR instead. BINARY – Used for data in Parquet. DATE – A date in ISO format, such as YYYY-MM-DD . For example, DATE '2008-09-15'. An exception is the OpenCSVSerDe, which uses the number of days elapsed since January 1, 1970.

Can I PARs multiple date formats in Athena?

Parsing Multiple Date Formats in Athena March 26, 2018/Alex Hague This article looks at how to use Amazon Athena with a column which contains dates in multiple different formats.

What is date_parse in Athena?

The Date_Parse Function in Athena Athena provides the date_parse function, this allows you to specify a string containing a date time and parse it using the provided format.

Why conversion failed when converting date and/or time from character string?

When we try to convert date or time from character string following error arises sometimes. “Conversion failed when converting date and/or time from character string.” The error mentioned above normally arises when the date literal is not proper and cannot be converted from the string into DateTime or date.


4 Answers

This worked -

cast(from_iso8601_timestamp(createdat) as date)
like image 196
Sagar Patil Avatar answered Oct 18 '22 04:10

Sagar Patil


The correct query for parsing a string into a date would be date_parse.

This would lead to the following query:

select date_parse(b.APIDT, '%Y-%m-%d') from APAPP100 b

prestodb docs: 6.10. Date and Time Functions and Operators

like image 35
jens walter Avatar answered Oct 18 '22 02:10

jens walter


The answer by @jens walter is great if you need to convert a column with a single date format in it. I've had situations where it's useful to have a column that contains multiple different date formats and still be able to convert it.

The following query supports a source column that contains dates in multiple different formats.

SELECT b.APIDT, 
   Coalesce(
     try(date_parse(b.APIDT, '%Y-%m-%d %H:%i:%s')),
     try(date_parse(b.APIDT, '%Y/%m/%d %H:%i:%s')),
     try(date_parse(b.APIDT, '%d %M %Y %H:%i:%s')),
     try(date_parse(b.APIDT, '%d/%m/%Y %H:%i:%s')),
     try(date_parse(b.APIDT, '%d-%m-%Y %H:%i:%s')),
     try(date_parse(b.APIDT, '%Y-%m-%d')),
     try(date_parse(b.APIDT, '%Y/%m/%d')),
     try(date_parse(b.APIDT, '%d %M %Y'))
   ) 
FROM APAPP100 b

The DATE_PARSE function performs the date conversion.

The TRY function handles errors by returning NULL if they do occur.

The COALESCE function takes the first non-null value.

There's a more in-depth write-up here (my blog).

like image 19
Alex Hague Avatar answered Oct 18 '22 04:10

Alex Hague


SELECT   b.APIDT,
         b.Appppppppp,
         date_diff('day',current_date, date(b.APIDT)) AS Duedays
FROM xyz100 a
WHERE regexp_like(b.apidt, '[0-9]{4}-[0-9]{2}-[0-9]{2}')

WHERE NOT regexp_like(b.apidt, '[0-9]{4}-[0-9]{2}-[0-9]{2}') to exclude the junk date

like image 1
vinsent paramanantham Avatar answered Oct 18 '22 02:10

vinsent paramanantham