cancel
Showing results for 
Search instead for 
Did you mean: 
Community 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’ .

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!