SQL EOMONTH function
Returns the last day of the month containing the input, shifted by a whole number of months if asked.
- Categoria
- Dates & times
- Returns
date- In this category
- 89 of 121
Sintaxe
EOMONTH(date [, months_to_add])
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sim | Supported under this name. |
| PostgreSQL | Different name or syntax | date_trunc('month', d) + INTERVAL '1 month -1 day' |
| MySQL | Different name or syntax | LAST_DAY(d) |
| SQLite | Different name or syntax | date(d, 'start of month', '+1 month', '-1 day') |
| Oracle | Different name or syntax | LAST_DAY(d) |
Exemplos
SQL Server
SELECT EOMONTH('2026-08-15'); -- 2026-08-31
SQL Server
SELECT EOMONTH('2026-08-15', -1); -- 2026-07-31
PostgreSQL
SELECT (date_trunc('month', DATE '2026-08-15') + INTERVAL '1 month -1 day')::date;
Bom saber
- The second argument is months, not days: EOMONTH('2026-08-15', -1) is July 31, 2026, the tidy way to get prior month-end for balances.
- EOMONTH returns the date type, with no time part even for datetime input; DATEADD(DAY, 1, EOMONTH(d)) is the first of the next month.
- It needs SQL Server 2012 or later. MySQL's LAST_DAY dates back to 4.1 and Oracle's is older still; neither takes a month offset.
Questions about EOMONTH
EOMONTH vs LAST_DAY?
They answer the same question; LAST_DAY exists in MySQL and Oracle, EOMONTH in SQL Server. The offset argument is EOMONTH's extra; LAST_DAY callers add DATE_ADD themselves.
How do I get the last *weekday* of the month?
No function does it. Step back from EOMONTH: subtract 1 day when it falls on a Saturday and 2 on a Sunday, testing DATEPART(WEEKDAY, ...) with SET DATEFIRST in mind.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Get the last day of the month in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Get the number of days in a month in SQL Snippet
- Get the same day in the previous month in SQL Snippet
- Get the first and last day of the month in SQL Snippet
- Get the first and last day of the quarter in SQL Snippet
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.