EOMONTH function in SQL
The EOMONTH function in SQL returns the last day of the month for a given date, with an optional parameter to specify the number of months to add or subtract.
SQL Syntax
EOMONTH(start_date, [month_to_add])
The following are the parameters of the function:
- start_date: The starting date (required). This can be a column or a specific date.
- month_to_add: Optional. Specifies how many months to add or subtract from the start_date. It can be positive (to go forward in time) or negative (to go back in time). If omitted, it defaults to 0, meaning it returns the last day of the month for the start_date.
Note: EOMONTH() returns a date in YYYY-MM-DD format.
Example: Get the Last Day of the Current Month.
SQL Syntax
SELECT EOMONTH(GETDATE()) AS LastDayOfCurrentMonth;
- GETDATE(): Retrieves the current date and time.
- No month_to_add specified, so it defaults to 0.
If today's date is 2024-10-10, the result will be:
LastDayOfCurrentMonth |
---|
2024-10-31 |