Skip to main content

try_to_date

This is a special version of try_to_date that performs the same operation, but returns a NULL value instead of raising an error if date cannot be created.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

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

Parameters

Parameter

Type

Description

col

pyspark.sql.Column or str

input column of values to convert.

format

literal string, optional

format to use to convert date values.

Returns

pyspark.sql.Column: date value as pyspark.sql.types.DateType type.

Examples

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('1997-02-28',)], ['ts'])
df.select('*', dbf.try_to_date(df.ts)).show()
df.select('*', dbf.try_to_date('ts', 'yyyy-MM-dd')).show()
df = spark.createDataFrame([('foo',)], ['ts'])
df.select(dbf.try_to_date(df.ts)).show()