Kontext Kontext / Code Snippets & Tips

Spark SQL - Get Next Monday, Tuesday, Wednesday, Thursday, etc.

event 2022-06-16 visibility 919 comment 0 insights toc
more_vert
insights Stats
toc Table of contents
Spark SQL - Get Next Monday, Tuesday, Wednesday, Thursday, etc.

Spark SQL provide a very convenient function named next_day since version 1.5.0. It can be used to calculate the next date that is after a certain date with specified name.

Syntax

next_day(start_date, day_of_week)

The function returns the first date which is later than start_date and named as indicated by day_of_week.

Code snippet

The following examples calculate any day of the week after.

spark-sql> select next_day(DATE'2022-06-16', 'Monday');
2022-06-20
spark-sql> select next_day(DATE'2022-06-16', 'TU');
2022-06-21
spark-sql> select next_day(DATE'2022-06-16', 'WE');
2022-06-22
spark-sql> select next_day(DATE'2022-06-16', 'TH');
2022-06-23
spark-sql> select next_day(DATE'2022-06-16', 'FR');
2022-06-17
spark-sql> select next_day(DATE'2022-06-16', 'FRIDAY');
2022-06-17
spark-sql> select next_day(DATE'2022-06-16', 'Sat');
2022-06-18
spark-sql> select next_day(DATE'2022-06-16', 'Sun');
2022-06-19

The function returns the right date per calendar:

2022061683025-image.png

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts