SQL - EOMONTH() Function



The SQL EOMONTH() function is used to return the last day of the month for a specified date value, with an optional offset.

Syntax

Following is the syntax of the SQL EOMONTH() function −

EOMONTH(start_date, [offset])

Parameters

This function accepts two parameters. The same is described below −

  • start_date − This specifies the date or datetime value for which we want to find the last day of the month.

  • offset − This specifies the number of months before or after the start date. This is an optional parameter. If the offset is omitted, the function returns the last day of the month.

Example

The following example demonstrates the usage of the SQL EOMONTH() function −

SQL> SELECT EOMONTH('2023-02-17') AS END_OF_MONTH;

Output

When we execute the above query, the output is obtained as follows −

+--------------+
| END_OF_MONTH |
+--------------+
| 2023-02-28   |
+--------------+

Example

We can use the GETDATE() function as a parameter to the EOMONTH() function, it retrieves the last day of the current month.

SQL> SELECT EOMONTH(GETDATE()) AS END_OF_MONTH;

Output

If we execute the above query, the result is produced as follows −

+--------------+
| END_OF_MONTH |
+--------------+
| 2023-02-28   |
+--------------+

Example

We can add 6 months to the last day of the current month using the following query −

SQL> SELECT EOMONTH(GETDATE(), 6) AS END_OF_MONTH;

Output

On executing the above query, the output is displayed as follows −

+--------------+
| END_OF_MONTH |
+--------------+
| 2023-08-31   |
+--------------+

Example

We can subtract 2 months from the last day of the current month using the following query −

SQL> SELECT EOMONTH(GETDATE(), -2) AS END_OF_MONTH;

Output

When we execute the above query, the output is obtained as follows −

+--------------+
| END_OF_MONTH |
+--------------+
| 2023-12-31   |
+--------------+

Example

In the following example, we are trying to retrieve the number of days in the current month using the following query −

SQL> SELECT DAY(EOMONTH(CURRENT_TIMESTAMP)) AS NO_OF_DAYS_IN_MONTH;

Output

If we execute the above query, the result is produced as follows −

+---------------------+
| NO_OF_DAYS_IN_MONTH |
+---------------------+
| 28                  |
+---------------------+

Example

Here, we are trying to retrieve the number of days from the specified date value using the following query −

SQL> SELECT DAY(EOMONTH('2023-04-13')) AS NO_OF_DAYS_IN_MONTH;

Output

When we execute the above query, the output is obtained as follows −

+---------------------+
| NO_OF_DAYS_IN_MONTH |
+---------------------+
| 30                  |
+---------------------+

Example

Assume we have created a table with the name CALENDAR in the SQL database using the CREATE statement as shown in the query below −

SQL> CREATE TABLE CALENDAR(MONTHS DATE NOT NULL);

Now, let us insert some records in the CALENDER table using INSERT statements as shown in the query below −

SQL> INSERT INTO CALENDAR (MONTHS) VALUES('2023-01-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-02-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-03-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-04-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-05-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-06-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-07-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-08-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-09-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-10-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-11-01');
INSERT INTO CALENDAR (MONTHS) VALUES('2023-12-01');

We can verify whether the table is created or not using the following query −

SQL> SELECT * FROM CALENDAR;

The table CALENDAR is successfully created in the SQL database.

+------------+
| MONTHS     |
+------------+
| 2023-01-01 |
| 2023-02-01 |
| 2023-03-01 |
| 2023-04-01 |
| 2023-05-01 |
| 2023-06-01 |
| 2023-07-01 |
| 2023-08-01 |
| 2023-09-01 |
| 2023-10-01 |
| 2023-11-01 |
| 2023-12-01 |
+------------+

We can display the total no of days in each calendar month using the following query −

SQL> SELECT MONTHS, DAY(EOMONTH(MONTHS)) AS NO_OF_DAYS_IN_MONTH FROM CALENDAR;

Output

When we execute the above query, the output is obtained as follows −

+------------+---------------------+
| MONTHS     | NO_OF_DAYS_IN_MONTH |
+------------+---------------------+
| 2023-01-01 | 31                  |
| 2023-02-01 | 28                  |
| 2023-03-01 | 31                  |
| 2023-04-01 | 30                  |
| 2023-05-01 | 30                  |
| 2023-06-01 | 31                  |
| 2023-07-01 | 30                  |
| 2023-08-01 | 31                  |
| 2023-09-01 | 30                  |
| 2023-10-01 | 31                  |
| 2023-11-01 | 30                  |
| 2023-12-01 | 31                  |
+------------+---------------------+

Example

Let us create another table with the name VEHICLE_RENTALS in the SQL database using the CREATE statement as shown in the query below −

SQL> CREATE TABLE VEHICLE_RENTALS(ID INT NOT NULL, CUSTOMER_NAME VARCHAR (200) NOT NULL, VEHICLE VARCHAR (200), RENTED_DATE DATE NOT NULL)

Now, let us insert some records in the VEHICLE_RENTALS table using INSERT statements as shown in the query below −

SQL> INSERT INTO VEHICLE_RENTALS(ID, CUSTOMER_NAME, VEHICLE, RENTED_DATE) VALUES(1, 'Dhruv', 'Activa', '2023-12-05');
INSERT INTO VEHICLE_RENTALS(ID, CUSTOMER_NAME, VEHICLE, RENTED_DATE) VALUES(2, 'Arjun','Access', '2023-03-01');
INSERT INTO VEHICLE_RENTALS(ID, CUSTOMER_NAME, VEHICLE, RENTED_DATE) VALUES(3, 'Dev','Jupiter', '2023-03-15');
INSERT INTO VEHICLE_RENTALS(ID, CUSTOMER_NAME, VEHICLE, RENTED_DATE) VALUES(4, 'Riya','Pept', '2023-12-05');
INSERT INTO VEHICLE_RENTALS(ID, CUSTOMER_NAME, VEHICLE, RENTED_DATE) VALUES(5, 'Aarohi','Vespa', '2023-05-02');
INSERT INTO VEHICLE_RENTALS(ID, CUSTOMER_NAME, VEHICLE, RENTED_DATE) VALUES(6, 'Lisa','Aviator', '2023-11-25');
INSERT INTO VEHICLE_RENTALS(ID, CUSTOMER_NAME, VEHICLE, RENTED_DATE) VALUES(7, 'Roy','Aprilia', '2023-05-30');

We can verify whether the table is created or not using the following query −

SQL> SELECT * FROM VEHICLE_RENTALS;

The table VEHICLE_RENTALS is successfully created in the SQL database.

+----+---------------+---------+-------------+
| ID | CUSTOMER_NAME | VEHICLE | RENTED_DATE |
+----+---------------+---------+-------------+
| 1  | Dhruv         | Activa  | 2023-12-05  |
| 2  | Arjun         | Access  | 2023-03-01  |
| 3  | Dev           | Jupiter | 2023-03-15  |
| 4  | Riya          | Pept    | 2023-12-05  |
| 5  | Aarohi        | Vespa   | 2023-05-02  |
| 6  | Lisa          | Aviator | 2023-11-25  |
| 7  | Roy           | Aprilia | 2023-05-30  |
+----+---------------+---------+-------------+

We can display the return date (End of month) of rented vehicles using the following query −

SQL> SELECT CUSTOMER_NAME, VEHICLE, RENTED_DATE, EOMONTH(RENTED_DATE) AS RETURN_DATE FROM VEHICLE_RENTALS;

Output

If we execute the above query, the result is produced as follows −

+----+---------------+---------+-------------+-------------+
| ID | CUSTOMER_NAME | VEHICLE | RENTED_DATE | RETURN_DATE |
+----+---------------+---------+-------------+-------------+
| 1  | Dhruv         | Activa  | 2023-12-05  | 2023-12-31  |
| 2  | Arjun         | Access  | 2023-03-01  | 2023-03-31  |
| 3  | Dev           | Jupiter | 2023-03-15  | 2023-03-31  |
| 4  | Riya          | Pept    | 2023-12-05  | 2023-12-31  |
| 5  | Aarohi        | Vespa   | 2023-05-02  | 2023-05-31  |
| 6  | Lisa          | Aviator | 2023-11-25  | 2023-11-30  |
| 7  | Roy           | Aprilia | 2023-05-30  | 2023-05-31  |
+----+---------------+---------+-------------+-------------+
sql-date-functions.htm
Advertisements