Skip to main content

try_to_number

Convert string 'col' to a number based on the string format format. Returns NULL if the string 'col' does not match the expected format. The format follows the same semantics as the to_number function.

For the corresponding Databricks SQL function, see try_to_number function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.try_to_number(col=<col>, format=<format>)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or str

Input column or strings.

format

pyspark.sql.Column or str, optional

format to use to convert number values.

Examples

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([("$78.12",)], ["e"])
df.select(dbf.try_to_number(df.e, dbf.lit("$99.99")).alias('r')).show()
origin = spark.conf.get("spark.sql.ansi.enabled")
spark.conf.set("spark.sql.ansi.enabled", "true")
try:
df = spark.range(1)
df.select(dbf.try_to_number(dbf.lit("77"), dbf.lit("$99.99")).alias('r')).show()
finally:
spark.conf.set("spark.sql.ansi.enabled", origin)