snowflake.snowpark.functions.try_to_numeric

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

A special version of TO_DECIMAL that returns NULL instead of raising an error if the conversion cannot be performed. Snowflake’s TRY_TO_DECIMAL only accepts string input. If scale is given without precision, the default precision of 38 is used. Also available as try_to_number and try_to_numeric.

Example:

>>> df = session.create_dataframe(['12', '11.3', 'abc'], schema=['a'])
>>> df.select(try_to_decimal(col('a'), 38, 0).as_('ans')).collect()
[Row(ANS=12), Row(ANS=11), Row(ANS=None)]
>>> df.select(try_to_decimal(col('a'), 38, 2).as_('ans')).collect()
[Row(ANS=Decimal('12.00')), Row(ANS=Decimal('11.30')), Row(ANS=None)]