'How to write below line from MySql using spark.sql? I am trying to Convert string to Date format

I need to write below code in Spark.Sql using python:

Input Column is in String Format

[Month/YearRecordDate]
Feb 2021

SELECT [Date] = CONVERT(DATE, fr.[Month/YearRecordDate]) AS Start_Date FROM Table1 fr

Output Column:

Start_Date
2021-02-01

So I tried below code but I got error.

df1 = spark.sql (f""" 
SELECT 
`Date` = CONVERT(DATE,`fr.Month/YearRecordDate`) FROM Table1 fr
""")

df1.display()

My questions are:

  1. Is it correct to give fr.Month/YearRecordDate?
  2. How to use CONVERT function using Spark SQL?
  3. How would you do it?


Solution 1:[1]

I tried this and it is working as intended.

df1 = spark.sql (f"""
SELECT `Month/YearRecordDate`, to_date(`Month/YearRecordDate`, 'MMM y')
FROM table1
""")

df1.display()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marrusama