Back to Library

DAY() in SQL

Extracts the day of the month (1-31) from a date or timestamp expression.

Syntax

DAY(date_expression) 
-- Some databases use: EXTRACT(DAY FROM date_expression)
Return type
INTEGER (1-31)

DAY() Function Example

SELECT DAY(order_date) FROM orders; 
-- Output: 26 (day extracted from date)

What is DAY() in SQL?

The DAY() function in SQL extracts the day of the month (1-31) from a given date or timestamp expression. It is commonly used for date filtering, scheduling, and time-based analysis.

This function is supported in multiple databases:

  • MySQL, PostgreSQL, and SQL Server: Use DAY(date_column).

  • Oracle: Uses EXTRACT(DAY FROM date_column).

  • SQLite: Uses strftime('%d', date_column).

Parameters:

  • date_expression: Date, datetime, or timestamp value

Example Use Cases:

-- Daily sales totals

SELECT DAY(sale_date) AS day, SUM(amount) FROM sales GROUP BY DAY(sale_date); 
-- Output:
day    sum_amount
---    ----------
1      45678.90
2      34567.89
3      56789.12
4      43210.98
5      54321.87
6      65432.10
7      76543.21
8      45678.90
9      34567.89
...    ...
30     56789.12
31     45678.90

-- Find end-of-month transactions 

SELECT * FROM transactions WHERE DAY(transaction_date) = DAY(LAST_DAY(transaction_date)); 
-- Output:
transaction_id    transaction_date    amount    status
--------------    ----------------    ------    -------
T145             2024-01-31          567.89    Complete
T167             2024-02-29          789.12    Complete
T189             2024-03-31          456.78    Pending
T234             2024-04-30          678.90    Complete
T256             2024-05-31          890.12    Processing

-- Get weekday analysis 

SELECT DAY(event_date) AS day_of_month, COUNT(*) AS events FROM events GROUP BY DAY(event_date);
-- Output:
day_of_month    events
------------    ------
1               45
2               38
3               42
4               39
5               41
...             ...
30              37
31              40

Notes:

  • Behavior: Returns NULL if input is NULL

  • Performance Considerations: May impact index usage

  • Version Info: Core function available in most DBMS

  • Deprecated/Recommended Alternatives: Some DBMS prefer EXTRACT()

Error Handling:

  • Error: Returns NULL for invalid dates

  • Recommendation: Validate date format before extraction

Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

DAY(), DAYOFMONTH()

DAY(date)

Returns NULL

PostgreSQL

EXTRACT(DAY FROM)

EXTRACT(DAY FROM date)

Returns NULL

SQL Server

DAY()

DAY(date)

Returns NULL

SQLite

strftime('%d')

strftime('%d', date)

Returns NULL

BigQuery

EXTRACT(DAY FROM)

EXTRACT(DAY FROM date)

Returns NULL

Snowflake

DAY()

DAY(date)

Returns NULL

Athena

EXTRACT(DAY FROM)

EXTRACT(DAY FROM date)

Returns NULL

Related Functions:

DATEFROMPARTS()

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

DATEFORMAT()

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

EOMONTH()

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

DAY()

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

DATEDIFF()

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

DATEADD()

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

Adds or subtracts a specified time interval from a date value.

GETDATE()

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

One place for all your queries,
directly on your SQL editor