Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get First date of month in Spark SQL?

I need to get the first date of month of any date using Spark SQL.

I have date column in my table

like image 977
Rajiv Singh Avatar asked Jan 25 '23 21:01

Rajiv Singh


2 Answers

date_add(last_day(add_months(<date_column>, -1)),1)
like image 65
Rajiv Singh Avatar answered Jun 27 '23 06:06

Rajiv Singh


In pyspark (>= v1.5) you can use the trunc function:

import pyspark.sql.functions as sf

df.withColumn('first_of_month', sf.trunc('date_col', 'month'))
like image 40
Willem Avatar answered Jun 27 '23 06:06

Willem