SQL days in month per engine
A per-engine recipe for the length of a month: build the month's last day, then read its day number.
- Kategorie
- Dates & times
- Returns
integer- In this category
- 81 of 121
Syntax
DAY(EOMONTH(d))
DAY(LAST_DAY(d))
EXTRACT(DAY FROM ...)
Each block is a separate form. The support table and the examples show which engine takes which.
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Different name or syntax | DAY(EOMONTH(d)) |
| PostgreSQL | Different name or syntax | EXTRACT(DAY FROM (date_trunc('month', d) + INTERVAL '1 month -1 day')) |
| MySQL | Different name or syntax | DAY(LAST_DAY(d)) |
| SQLite | Different name or syntax | CAST(strftime('%d', date(d, 'start of month', '+1 month', '-1 day')) AS INTEGER) |
| Oracle | Different name or syntax | EXTRACT(DAY FROM LAST_DAY(d)) |
Beispiele
SQL Server
SELECT DAY(EOMONTH('2026-02-15')); -- 28
Oracle
SELECT EXTRACT(DAY FROM LAST_DAY(DATE '2028-02-15')) FROM dual; -- 29
SQLite
SELECT strftime('%d', date('2026-02-15', 'start of month', '+1 month', '-1 day')); -- 28
Gut zu wissen
- February is the only variable: the recipes return 29 for Feb 2028 and 28 for Feb 2026 automatically; never hard-code 28/29 switches.
- On SQLite the '+1 month' then '-1 day' chain lands on the month end safely even from the 31st, because it walks from the month's first day.
- None of the engines has a named function for this; the one-liners are the idiom, and LAST_DAY/EOMONTH are the shared half of it.
Questions about Days in month
What is the shortest portable version?
There is none. Every recipe has the same shape (build the month's last day, read its day number), so keep your engine's one-liner rather than a CASE table of month lengths.
How do I get days in a quarter or year?
Subtract period starts instead of reading DAY(): on PostgreSQL (date_trunc('quarter', d) + INTERVAL '3 months')::date - date_trunc('quarter', d)::date is 92 for Q3; on SQL Server 2022+ DATEDIFF(DAY, DATETRUNC(QUARTER, d), DATEADD(QUARTER, 1, DATETRUNC(QUARTER, d))).
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
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.