Spark SQL - Convert String to Timestamp

Raymond Raymond event 2021-01-09 visibility 5,493
more_vert

Similar as Convert String to Date using Spark SQL, you can convert string of timestamp to Spark SQL timestamp data type.

Function to_timestamp

Function to_timestamp(timestamp_str[, fmt]) parses the `timestamp_str` expression with the `fmt` expression to a timestamp data type in Spark. 

Example:

spark-sql> select to_timestamp('2021-01-09 17:34:59');
to_timestamp('2021-01-09 17:34:59')
2021-01-09 17:34:59

In the above example, the string is in default format of timestamp, thus there is no need to pass in the format parameter.  If it is not in the default format, you need to pass in the format string.

Example:

spark-sql> select to_timestamp('2021010917:34:59','yyyyMMddHH:mm:ss');
to_timestamp('2021010917:34:59', 'yyyyMMddHH:mm:ss')
2021-01-09 17:34:59

All the datetime patterns are list in Datetime patterns.

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts