0

I need to change string into date format 'YYYY-MM-DD'
Input data is

20180716
20180912

like this in string type and need output as

2018-07-16
2018-09-12

spark.sql("""select TO_DATE(CAST(UNIX_TIMESTAMP(MYDAT, 'yyyyMMdd') AS TIMESTAMP)) AS reg_datefrom Table  """)

query for above output is

reg_date
null    
null    
null    
null    
null

But when I am writing below code its working.

spark.sql("""
  SELECT TO_DATE(CAST(UNIX_TIMESTAMP('20180811', 'yyyyMMdd') AS TIMESTAMP)) AS newdate"""
).show()

Output:

+----------+ | newdate| +----------+ |2018-08-11| +----------+

Where I am doing mistake.

2
  • show the output of query select MYDAT,TO_DATE(CAST(UNIX_TIMESTAMP(MYDAT, 'yyyyMMdd') AS TIMESTAMP)) AS reg_date from Table Commented Nov 27, 2018 at 9:54
  • Do you have MYDAT column in table Commented Nov 27, 2018 at 10:46

2 Answers 2

0

In Spark, we can write the TO_DATE('20180811', 'yyyyMMdd') function by passing the date format of the string value that passing to TO_DATE function and returns the date as yyyy-MM-dd

Sign up to request clarification or add additional context in comments.

Comments

0

I have tried this at my end, this is working however I feel 2 possibilities looking at your snippet

  1. Please check your MYDAT column is string
  2. in ur query reg_datefrom is concatnated, seprate it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.