I am new to Spark SQL. We are migrating data from SQL server to Databricks. I am using SPARK SQL . Can you please suggest how to achieve below functionality in SPARK sql for the below datefunctions. I can see datediff gives only days in spark sql.
DATEDIFF(YEAR,StartDate,EndDate) DATEDIFF(Month,StartDate,EndDate) DATEDIFF(Quarter,StartDate,EndDate)
As you have mentioned SparkSQL does support
DATEDIFF
but for days only. I would also be careful as it seems the parameters are the opposite way round for Spark, ieSpark does however support a similar function called
months_between
which you could use in place ofDATEDIFF( month ...
. This function also returns a decimal amount so optionally cast it toINT
for similar functionality to theThere are also
year
andquarter
functions for determining the year and quarter of a date respectively. You could simply minus the years but quarters would be more tricky. It may be you have to 'do the math' or end up using a calendar table.