SQL MONTHNAME function
Returns the month name in the session language: a display concern, which is why SQLite's format codes skip it entirely.
- Catégorie
- Dates & times
- Returns
text- In this category
- 57 of 121
Syntaxe
MONTHNAME(d) -- MySQL
DATENAME(month, d) -- SQL Server
TO_CHAR(d, 'Month') -- PostgreSQL, Oracle
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Different name or syntax | DATENAME(month, d) |
| PostgreSQL | Different name or syntax | TO_CHAR(d, 'FMMonth') to trim padding |
| MySQL | Oui | Supported under this name. |
| SQLite | Not built in | no month-name format; map with CASE |
| Oracle | Different name or syntax | TO_CHAR(d, 'FMMonth'); follows NLS_DATE_LANGUAGE |
Exemples
MySQL
SELECT MONTHNAME(created_at) AS mon FROM orders;
SQL Server
SELECT DATENAME(month, created_at) AS mon FROM orders;
PostgreSQL
SELECT TO_CHAR(created_at, 'FMMonth') AS mon FROM orders;
Oracle
SELECT TO_CHAR(created_at, 'FMMonth') AS mon FROM orders;
Bon à savoir
- PostgreSQL and Oracle pad the name to nine characters: TO_CHAR(d, 'Month') gives 'January '. The FM prefix ('FMMonth') trims it on both.
- Names follow the session language: Oracle's NLS_DATE_LANGUAGE and SQL Server's LANGUAGE flip 'January' to other words per connection.
- Do not sort or join on names: 'April' sorts before 'January'. Keep the number and format at the edge.
Questions about MONTHNAME
Why is my month name padded with spaces?
PostgreSQL and Oracle pad to the longest month name (nine characters) for alignment. The FM modifier, as in TO_CHAR(d, 'FMMonth'), removes it on both.
How do I get month names in SQLite?
There is no format code for names; CASE the month number or map it in the application.
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, 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.