Presto
Athena
string conversion
timestamp
SQL functions

How to convert string into timestamp in Presto Athena?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In Presto and Athena, the right string-to-timestamp function depends on the input format. ISO 8601 strings are usually handled with dedicated functions, while custom formats need parsing functions such as date_parse or parse_datetime.

Use from_iso8601_timestamp for ISO Strings

If the string is already in ISO 8601 format, this is usually the cleanest option.

sql
SELECT from_iso8601_timestamp('2024-07-01T12:34:56Z');

That returns a timestamp with time zone, which is exactly what you usually want for a fully qualified ISO instant.

Use date_parse for Custom Formats

For strings that are not ISO 8601, date_parse is the common tool.

sql
SELECT date_parse('2024-07-01 12:34:56', '%Y-%m-%d %H:%i:%s');

This returns a timestamp value parsed according to the format string.

Common format pieces include:

  • '%Y four-digit year'
  • '%m month'
  • '%d day of month'
  • '%H hour in 24-hour time'
  • '%i minute'
  • '%s second'

The parser only works if the format string matches the input exactly.

Use parse_datetime When Time Zone Parsing Matters

If the source string contains time-zone information and you want a timestamp with time zone, parse_datetime is often the better match.

sql
SELECT parse_datetime('2024-07-01 12:34:56 UTC', 'yyyy-MM-dd HH:mm:ss z');

This is especially useful when the input contains explicit zone text rather than a plain local timestamp string.

Casting Works Only for Simple Cases

Sometimes a direct cast is enough.

sql
SELECT CAST('2024-07-01 12:34:56' AS timestamp);

This is convenient, but it is much less explicit than the parsing functions and can be fragile when the input format varies. For ingestion queries, explicit parsing is usually safer.

Athena and Presto Behavior Depends on the Engine

Athena uses a Presto- or Trino-style SQL engine, but exact behavior can vary by engine version. That is why explicit parsing functions are better than relying on implicit casts or assumptions from old blog posts.

If a query behaves differently than expected, check both the input format and the engine version your Athena workgroup is using.

Parse First, Then Normalize

A good pattern is to parse the timestamp once and normalize it in the same query if needed.

sql
1WITH src AS (
2    SELECT '2024-07-01 12:34:56' AS raw_ts
3)
4SELECT date_parse(raw_ts, '%Y-%m-%d %H:%i:%s') AS parsed_ts
5FROM src;

That keeps the transformation readable and makes debugging easier when bad rows appear.

Test the Parser on a Few Real Rows First

A small SELECT against representative raw values is usually the fastest way to confirm the format string before you run a larger query. Timestamp parsing errors are much easier to fix on five sample values than inside a long production query with several joins and transformations layered on top.

That small verification step pays for itself quickly.

Treat Time Zone Assumptions Explicitly

If the source string does not include a time zone, the query still needs a clear interpretation of what that timestamp means. Ambiguous local times are a common source of downstream reporting bugs, even when the parser itself succeeds.

Common Pitfalls

The biggest mistake is using the wrong parsing function for the input shape. ISO 8601 input and custom-formatted input should not be treated the same.

Another issue is mismatching the format tokens in date_parse, especially minutes versus months or 12-hour versus 24-hour time.

A third problem is relying on implicit CAST behavior when the source strings are not perfectly uniform.

Summary

  • Use from_iso8601_timestamp for ISO 8601 strings.
  • Use date_parse for custom timestamp formats.
  • Use parse_datetime when parsing time-zone-aware text matters.
  • Prefer explicit parsing over implicit casts in ingestion queries.
  • When debugging Athena behavior, consider both the string format and the engine version.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.