MySQL DAY() Function
The DAY() function in MySQL extracts the day (as a number) from a given date or datetime value. It returns an integer between 1 and 31, representing the day of the month.
Syntax
Parameter:
date: A validDATE,DATETIME, orTIMESTAMPvalue from which the day will be extracted.
Return Value:
- Returns an integer (1-31) representing the day of the month.
- Returns
NULLif the input isNULLor an invalid date.
Examples
1. Extracting the Day from a Date
Output:
2. Extracting the Day from a Datetime
Output:
3. Using DAY() on a Table Column
Assume we have an orders table:
This query will return the day of the month for each order.
4. Using DAY() in a WHERE Clause
Find all records from the 10th day of any month:
Handling NULL and Invalid Dates
Alternative: Extracting Full Date Components
If you need more than just the day, you can use:
MONTH()– Extracts the month number.YEAR()– Extracts the year.DAYOFWEEK()– Returns the day of the week (1 = Sunday, 7 = Saturday).
Example:
Output:
Conclusion
DAY()extracts the day from a date (1-31).- Useful for filtering and grouping data by day.
- Can be combined with
MONTH()andYEAR()for advanced queries.
Great for date-based analysis in MySQL! š

