The DAY() function in MySQL is used to return the day of the month from a given date or datetime value. The result is an integer between 1 and 31, representing the day of the month.
Definition and Usage
- The
DAY()function returns the day part of a given date. This is essentially the day of the month (from 1 to 31). - Note: This function is equivalent to the
DAYOFMONTH()function.
Syntax
DAY(date)
- date: Required. A valid date or datetime value from which the day will be extracted.
Examples
Example 1: Return the day of the month for a specific date
SELECT DAY("2017-06-15");
Output:
15
This query returns 15, as it is the day of the month in the date 2017-06-15.
Example 2: Return the day of the month for a datetime value
SELECT DAY("2017-06-15 09:34:21");
Output:
15
Even though the datetime contains both date and time, the DAY() function returns only the day part (15).
Example 3: Return the day of the month for the current system date
SELECT DAY(CURDATE());
Output:
28
Assuming today's date is 2024-12-28, this query will return 28, as it is the current day of the month.
Use Cases
- Extracting Day of the Month: Useful when you only need the day portion from a date or datetime value.
- Reports and Aggregations: You can use
DAY()in queries to group or filter data by specific days. - Date Calculations: When performing date-related calculations that involve the day of the month.
Related Functions
DAYOFMONTH(): This function is equivalent toDAY().DAYOFWEEK(): Returns the weekday index (1 for Sunday through 7 for Saturday).DAYOFYEAR(): Returns the day of the year (1 through 366).MONTH(): Extracts the month from a date.YEAR(): Extracts the year from a date.