// dates & times reference

SQL MONTHNAME function

Returns the month name in the session language: a display concern, which is why SQLite's format codes skip it entirely.

Categoria
Dates & times
Returns
text
In this category
57 of 121

Sintaxe

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.

Suporte a mecanismos

MotorSupportNotas
SQL Server Different name or syntax DATENAME(month, d)
PostgreSQL Different name or syntax TO_CHAR(d, 'FMMonth') to trim padding
MySQL Sim 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

Exemplos

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;

Bom saber

  • 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.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

// fewer lookups

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.