Spark SQL - Convert String to Timestamp

Raymond Tang Raymond Tang 0 5964 3.66 index 1/9/2021

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.

spark-sql spark-sql-function

Join the Discussion

View or add your thoughts below

Comments