The MySQL DAY() function is used to extract the day of the month (a number from 1 to 31) from a given date.
Syntax:
DAY(date)
Parameters:
date: A valid date, datetime, or a string in a valid date format.
Returns:
- An integer value representing the day of the month.
Examples:
-
Extract the day from a date:
SELECT DAY('2024-12-23') AS DayOfMonth;Output:
DayOfMonth 23 -
Using with a datetime value:
SELECT DAY('2024-12-23 15:30:00') AS DayOfMonth;Output:
DayOfMonth 23 -
Using a column from a table:
SELECT order_id, DAY(order_date) AS DayOfMonth FROM orders; -
Invalid date input: If the
dateprovided is invalid,DAY()will returnNULL.SELECT DAY('invalid-date') AS DayOfMonth;Output:
DayOfMonth NULL
Notes:
- It is often used in conjunction with other date and time functions in MySQL.
- The function is case-insensitive, so
day()works the same asDAY().
If you need to extract a specific part of the date, such as the year or month, you can use related functions like YEAR(), MONTH(), etc.
No comments:
Post a Comment