snowflake.snowpark.functions.try_to_timestamp

snowflake.snowpark.functions.try_to_timestamp(e: Union[Column, str], fmt: Optional[Union[Column, str]] = None) → Column[source]

A special version of TO_TIMESTAMP that returns NULL instead of raising an error if the conversion cannot be performed. Snowflake’s TRY_TO_TIMESTAMP only accepts string input. The format has to be specified according to the rules set forth in <https://docs.snowflake.com/en/sql-reference/functions-conversion#date-and-time-formats-in-conversion-functions>

Example:

>>> df = session.create_dataframe(['2019-01-31 01:02:03.004', 'not a timestamp'], schema=['a'])
>>> df.select(try_to_timestamp(col('a')).as_('ans')).collect()
[Row(ANS=datetime.datetime(2019, 1, 31, 1, 2, 3, 4000)), Row(ANS=None)]
>>> df.select(try_to_timestamp(col('a'), 'YYYY-MM-DD HH24:MI:SS.FF3').as_('ans')).collect()
[Row(ANS=datetime.datetime(2019, 1, 31, 1, 2, 3, 4000)), Row(ANS=None)]