access_time 7 months ago languageEnglish

Spark SQL - Convert String to Timestamp

visibility 281 comment 0

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. 


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.


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.

copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn.

Want to contribute on Kontext to help others?

Learn more

More from Kontext

visibility 208
thumb_up 0
access_time 7 months ago
visibility 210
thumb_up 0
access_time 7 months ago
visibility 59
thumb_up 0
access_time 7 months ago