cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

date type conversion error

ashkd7310
New Contributor II

Hello,

I am trying to convert the date in MM/dd/yyyy format. So I am first using the date_format function and converting the date into MM/dd/yyyy. So it becomes string. However, my use case is to have the data as date. so I am again converting the string into date by using to_date function. However, it becomes null when I am doing that way. 

I have tried many ways but unable to solve. Is this something databricks doesn't support to store the date in the format of  MM/dd/yyyy?

2 REPLIES 2

szymon_dybczak
Contributor III

Hi @ashkd7310 ,

It's not possible. Spark does not support date types formatted in some other way except for yyyy-MM-dd. If you need another format, you will need to again convert the date type into string type, but with the format which you need.

 

 

Rishabh-Pandey
Esteemed Contributor

Check with this method if it works.

# Convert date to MM/dd/yyyy format (string)
df = df.withColumn("formatted_date", date_format("date", "MM/dd/yyyy"))

# Convert string back to date
df = df.withColumn("converted_date", to_date("formatted_date", "MM/dd/yyyy"))

 

 

Rishabh Pandey

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group