// dates & times reference

SQL DAYNAME function

Returns the weekday name in the session language, which sidesteps the engines' disagreement over whether Sunday is day 0, 1 or 7.

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

Sintaxe

DAYNAME(d)  -- MySQL
DATENAME(weekday, d)  -- SQL Server
TO_CHAR(d, 'Day')  -- 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(weekday, d)
PostgreSQL Different name or syntax TO_CHAR(d, 'FMDay') trims padding
MySQL Sim Supported under this name.
SQLite Not built in no weekday-name format; CASE over strftime('%w')
Oracle Different name or syntax TO_CHAR(d, 'Day') follows NLS settings

Exemplos

MySQL

SELECT DAYNAME(created_at) AS weekday FROM orders;

SQL Server

SELECT DATENAME(weekday, created_at) AS weekday FROM orders;

PostgreSQL

SELECT TO_CHAR(created_at, 'FMDay') AS weekday FROM orders;

Bom saber

  • PostgreSQL and Oracle pad to nine characters: TO_CHAR(d, 'Day') returns 'Monday '. FMDay trims it on both.
  • Language follows the session: SQL Server's LANGUAGE and Oracle's NLS_DATE_LANGUAGE change the words, so never filter on the English spelling.
  • Weekday names do not sort or join well; keep the underlying dow or isodow number for logic and format for display.

Questions about DAYNAME

Why does TO_CHAR(d, 'Day') add spaces?

PostgreSQL and Oracle pad weekday names to nine characters. Prefix the format with FM (FMDay) or RTRIM the result.

How do I filter for weekends by name?

Do not compare names: use EXTRACT(isodow) IN (6, 7) on PostgreSQL, WEEKDAY(d) >= 5 on MySQL; name checks are for display only.

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.