// dates & times reference

SQL DAY function

Returns the day-of-month number from a date, useful for month-end logic like flagging entries on or after the 15th.

Categoría
Dates & times
Returns
integer
In this category
39 of 121

Sintaxis

DAY(d)
EXTRACT(day FROM d)
strftime('%d', d)  -- SQLite, zero-padded text

Each block is a separate form. The support table and the examples show which engine takes which.

Soporte del motor

MotorSupportNotas
SQL Server Sí Supported under this name.
PostgreSQL Different name or syntax EXTRACT(day FROM d)
MySQL Sí Supported under this name.
SQLite Different name or syntax strftime('%d', d) returns text
Oracle Different name or syntax EXTRACT(day FROM d); there is no DAY() function

Ejemplos

PostgreSQL

SELECT EXTRACT(day FROM created_at) AS dom FROM orders;

SQL Server

SELECT DAY(created_at) AS dom FROM orders;

SQLite

SELECT CAST(strftime('%d', created_at) AS INTEGER) AS dom FROM orders;

Conviene saberlo

  • DAY is day of month, not day of week or day of year; those are DATEPART(weekday) and DATEPART(dayofyear) on SQL Server.
  • SQLite's %d is zero-padded text: '01' through '31', so CAST before arithmetic.
  • MySQL accepts DAY(), DAYOFMONTH() and the ANSI EXTRACT, all identical; older scripts use DAYOFMONTH.

Questions about DAY

What is the difference between DAY and DAYOFYEAR?

DAY returns 1-31 within the month; DAYOFYEAR returns 1-366 within the year. MySQL even spells them DAYOFMONTH and DAYOFYEAR to keep it clear.

Does Oracle have DAY()?

No. SELECT DAY(SYSDATE) FROM dual raises ORA-00904. Use EXTRACT(day FROM d), or TO_CHAR(d, 'DD') for zero-padded text.

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, SQLite, 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.