The DAYOFMONTH() function in MySQL returns the day of the month for a given date or datetime value. The result is a number between 1 and 31 representing the day of the month.
Definition and Usage
- The
DAYOFMONTH()function returns the day of the month from a given date. - It is equivalent to the
DAY()function.
Syntax
DAYOFMONTH(date)
- date: Required. A valid date or datetime value for which the day of the month is to be extracted.
Examples
Example 1: Return the day of the month for a specific date
SELECT DAYOFMONTH("2017-06-15");
Output:
15
This query returns 15 because the day of the month for the date 2017-06-15 is 15.
Example 2: Return the day of the month for a datetime value
SELECT DAYOFMONTH("2017-06-15 09:34:21");
Output:
15
Even though the datetime contains both date and time, the DAYOFMONTH() function extracts only the day of the month (15).
Example 3: Return the day of the month for the current system date
SELECT DAYOFMONTH(CURDATE());
Output:
28
Assuming today's date is 2024-12-28, this query returns 28 as it is the current day of the month.
Use Cases
- Extracting the Day: Useful when you need to work with just the day of the month, not the entire date.
- Reports and Data Analysis: Can be used to group or filter data based on the day of the month.
- Scheduling: Useful for applications that track specific dates and require extracting the day of the month for calculations or comparisons.
Related Functions
DAY(): Equivalent toDAYOFMONTH(), returns the day of the month.DAYOFWEEK(): Returns the day of the week (1 for Sunday through 7 for Saturday).DAYOFYEAR(): Returns the day of the year (1 through 366).MONTH(): Returns the month from a given date.