SQL DATENAME function
Returns the named part of a date as text: DATENAME(month, d) gives 'January' where DATEPART(month, d) gives 1.
- Categoría
- Dates & times
- Returns
text- In this category
- 59 of 121
Sintaxis
DATENAME(month, d)
DATENAME(weekday, d)
DATENAME(year, d) -- text, not int
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | Supported under this name. |
| PostgreSQL | Different name or syntax | TO_CHAR with Month / Day masks |
| MySQL | Different name or syntax | MONTHNAME and DAYNAME for the word parts |
| SQLite | Not built in | strftime formats; no names |
| Oracle | Different name or syntax | TO_CHAR |
Ejemplos
SQL Server
SELECT DATENAME(month, created_at) AS mon FROM orders;
SQL Server
SELECT DATENAME(weekday, GETDATE()) AS today_name;
SQL Server
SELECT DATEPART(month, GETDATE()) AS mon_num; -- int
Conviene saberlo
- Everything comes back as nvarchar: DATENAME(day, d) is '5', which sorts after '10' and needs a cast to join to an int. Use DATEPART for numbers.
- Month and weekday names follow the session LANGUAGE setting; the same query returns Spanish or German words elsewhere.
- For week numbers DATENAME returns the text of DATEPART(week)'s convention, not ISO. Use DATENAME(iso_week) explicitly.
Questions about DATENAME
DATENAME or DATEPART?
DATENAME for display words, DATEPART for numbers you filter, sort or group on. ORDER BY DATENAME(month, d) puts April first and January fifth.
Does DATENAME respect SET LANGUAGE?
Yes. Month and weekday names come out in the session language, so reports can localize but filters on English names can silently fail.
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.