How to use the MySQL DAY() function
The DAY()
function in MySQL is used to get the day of the month from a date value. It returns the day number (1-31) for the given date.
The DAY()
function in MySQL is used to get the day of the month from a date value. It returns the day number (1-31) for the given date.
Syntax
The syntax for DAY()
is:
DAY(date)
Where date
is the date value to extract the day from.
Examples
-
Get the day of the month from a date:
SELECT DAY('2023-02-15');
Returns 15, the day portion of the date.
-
Use DAY() to filter dates:
SELECT * FROM orders WHERE DAY(order_date) = 1;
Returns orders placed on 1st day of the month.
-
Get the day from a date column:
SELECT DAY(birthday) FROM users;
Returns just the day part of each user’s birthday.
-
Use DAY() in a WHERE clause:
SELECT * FROM billing WHERE DAY(billing_date) BETWEEN 1 AND 15;
Returns rows where billing day is between 1-15.
-
Get day name from date using WEEKDAY():
SELECT WEEKDAY('2023-02-15');
Returns 3, corresponding to Wednesday.
Other Similar Functions
WEEKDAY()
- Day name from dateDAYOFWEEK()
- Index of day in weekDAYOFMONTH()
- Day of month (same asDAY()
)DAYOFYEAR()
- Day number in year
So DAY()
provides an easy way to get the day from a date in MySQL queries.