cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert string date to date after changing format

shanmukh_b
New Contributor

Hi,

I am using Data bricks SQL and came across a scenario. I have a date field whose dates are in format of 'YYYY-MM-DD'. I changed their format into 'MM/DD/YYYY' using DATE_FORMAT() function.

EFF_DT = 2000-01-14

 

 

 

EFF_DT _2 = DATE_FORMAT(EFF_DT, 'MM/dd/yyyy') 

 

 

 

DATE_FORMAT() returns a string with with the format 01/14/2000. Now when I am trying to convert this string back to date with the same format using TO_DATE function, I am getting 2000-01-14 instead of 01/14/2000.

 

 

 

EFF_DT _3 =  TO_DATE(DATE_FORMAT(EFF_DT, 'MM/dd/yyyy'), 'MM/dd/yyyy')

 

 

 

I want eff_dt_3 = 01/14/2000 as date. Can anyone help me out.

 

EFF_DTEFF_DT_2EFF_DT_3
2000-01-1401/14/20002000-01-14

 

1 REPLY 1

-werners-
Esteemed Contributor III

if you use to_date, you will get a date column as mentioned above.
If you want to use the format MM/dd/yyyy you can use date_format but this will return a string column.

In order to use Spark date functions, Date string should comply with Spark DateType format which is ‘yyyy-MM-dd’ .

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