- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
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 | +----+---------------+---------+-------------+-------------+
To Continue Learning Please Login
Login with Google