When I query a column of TIMESTAMP data in my Amazon Athena table, I get empty results or the query fails. The data exists in the input file. How do I resolve this?

The TIMESTAMP data might be in the wrong format. Athena requires the Java TIMESTAMP format:

YYYY-MM-DD HH:MM:SS.fffffffff

To change TIMESTAMP data to the correct format:

  1. Define the column as STRING.
  2. Use Presto's date and time functions to read the column as DATE or TIMESTAMP in your query.

Define the column as STRING

Run the following command:

CREATE EXTERNAL TABLE 'test'(
  'ts' STRING)ROW FORMAT SERDE<  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
LOCATION
  's3://<your-bucket>/<path-to-data>/'

Use Presto's date and time functions to read the column as DATE or TIMESTAMP in your query

If your input data is in the following format:

20170621T015005
20170723T095605

Use the date_parse function as shown in the following example:

SELECT date_parse(ts,'%Y%m%dT%h%i%s') FROM timestamptestcsv3

Note: The format in the date_parse(string,format) function must be the TIMESTAMP format that's used in your data. For more information, see MySQL Date Functions in Presto Documentation.

If your input data is in ISO 8601 format, as in the following:

2018-12-24T02:34:48Z

Use the from_iso8601_timestamp() function to read the ts field as TIMESTAMP. Example:

SELECT from_iso8601_timestamp(ts) FROM timestamptestcsv3

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2018-08-08

Updated: 2019-01-17