Srikant
Srikant

Reputation: 437

Fetch Weekend date from a date column using spark

I need to get the weekend day ( Saturdays date ) from a date column using spark SQL .

For e.g

If i provide a date "2020-01-10" it should return "2020-01-11" If i provide a date "2020-01-01" it should return "2020-01-04"

Upvotes: 2

Views: 839

Answers (2)

Srikant
Srikant

Reputation: 437

I am using

cast(date_add(date_Column, 6-cast(date_format(date_Column, 'u') as BIGINT)) as date) weekend_day

Upvotes: 0

Vapira
Vapira

Reputation: 121

From Spark 2.3.0 you can use date_trunc to get beginning of week date, and then add number of days to get to desired week day.

spark.sql("select '2020-01-10' given, date_trunc('WEEK', '2020-01-10') week_start , date_add(date_trunc('WEEK', '2020-01-10'),5) saturday_date").show()
+----------+-------------------+-------------+
|     given|         week_start|saturday_date|
+----------+-------------------+-------------+
|2020-01-10|2020-01-06 00:00:00|   2020-01-11|
+----------+-------------------+-------------+

Upvotes: 2

Related Questions