NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL

For Complete YouTube Video: Click Here

In this class, we will understand NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL.

We have already discussed the concepts of ADD_MONTHS GREATEST LEAST TO_DATE Functions in SQL.

NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL

To understand this, we will consider the holiday table as shown below.

NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL 1
NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL 1

NEXT_DAY

NEXT_DAY Computes the date of the ‘next’ named week.

The syntax for the NEXT_DAY function is as shown below.

NEXT_DAY(Date, ‘FRIDAY’)

From the above example, we can find the next date of the day from the given date mentioned in the function.

The query and the output are as shown below.

SELECT festival, NEXT_DAY(startdate, ‘FRIDAY’) FROM holidays;

NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL 2
NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL 2

LAST_DAY

LAST_DAY produces the day of the last date of the month.

SELECT festival, LAST_DAY(startdate) FROM holidays;

The query and the output are as shown below.

NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL 3
NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions 3

MONTHS_BETWEEN

To understand this function, we will consider the employee table as shown below.

NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL 4
NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions 4

MONTHS_BETWEEN is used to find the number of months between the dates.

From the above table, we will consider the date of birth and the current date to get the employee’s age.

To round off the value, we use the Ceil function.

The query and the output are as shown below.

NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions in SQL 5
NEXT_DAY LAST_DAY MONTHS_BETWEEN Functions 5