Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Athena SQL Query Error with timestamp

I have a table in AWS Athena having column named 'servertime' with the data type of timestamp. I run a query like this

select * 
from table_name 
where servertime between '2018-04-01 00:00:00' and '2018-04-05 23:59:59';

It gives me this error: Your query has the following error(s): SYNTAX_ERROR: line 1:41: '=' cannot be applied to timestamp, varchar(19)

How can I resolve this in Athena? And It's important query to get the data from this table.

like image 777
Sumit Kumar Sagar Avatar asked Aug 09 '18 07:08

Sumit Kumar Sagar


People also ask

How do I use a timestamp in Athena?

The TIMESTAMP data in your table might be in the wrong format. Athena requires the Java TIMESTAMP format. Use Presto's date and time function or casting to convert the STRING to TIMESTAMP in the query filter condition. For more information, see Date and time functions and operators in the Presto documentation.

How do you write the date in Athena?

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.

Why do I get zero records when I query my Amazon Athena table?

Incorrect LOCATION path If the input LOCATION path is incorrect, then Athena returns zero records.

Is Athena SQL case sensitive?

Athena accepts mixed case in DDL and DML queries, but lower cases the names when it executes the query. For this reason, avoid using mixed case for table or column names, and do not rely on casing alone in Athena to distinguish such names.


1 Answers

The problem you are seeing is related to your between condition. If you present the timestamp just as varchar, Athena will not convert those into timestamps.

For that to happen, you need to pass an explicit typecast.

select * from table_name 
where servertime 
   between TIMESTAMP '2018-04-01 00:00:00' 
   and TIMESTAMP '2018-04-05 23:59:59';
like image 114
jens walter Avatar answered Sep 22 '22 12:09

jens walter