User16857282152
Databricks Employee
Databricks Employee

Here is a working example,

In SQL,

create table temp1 as (select 20010101.00 date);

-- This creates a table with a single column named "date" with a datatype of decimal.

-- To verify run "describe temp1";

the to_date function takes a string as an input so first cast the decimal to string.

select cast(date as String) from temp1;

Once you have a string you can push to a date,

select to_date(cast(date as String), 'yyyyMMdd') date from temp1;

You could do the same using dataframe api.

df1 = spark.sql("select 20010101.00 date")

Convert to string

df2 = df1.select(df1.date.cast("string"))

Drop right of decimal

from pyspark.sql.functions import substring_index

df3 = df2.select(substring_index(df2.date, '.', 1).alias('date'))

Convert String to Date

from pyspark.sql.functions import to_date

df4 = df3.select(to_date('date', 'yyyyMMdd').alias('date'))