DAY() in SQL
Extracts the day of the month (1-31) from a date or timestamp expression.
Syntax
Return type
DAY() Function Example
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
-- Find end-of-month transactions
-- Get weekday analysis
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