MySQL DAY

The MySQL DAY function is a date and time function that extracts the day of the month from a given date. It is commonly used to retrieve the day part from a date expression in order to perform various operations or to display specific information based on the day.

Syntax

Here is the basic syntax of the DAY function:

DAY(date)

date: This is the date or date expression from which you want to extract the day of the month.
The function returns an integer representing the day of the month.

Example

Let’s look at a simple example:

SELECT DAY('2024-01-01') AS DayOfMonth;

In this example, the DAY function extracts the day from the date ‘2024-01-01’, and the result would be:

+------------+
| DayOfMonth |
+------------+
|          1 |
+------------+

You can also use the DAY function with a column in a table. For instance:

SELECT event_name, DAY(event_date) AS EventDay
FROM events;

Assuming there is a table named events with columns event_name and event_date, this query retrieves the event names along with the corresponding days of the month.

Keep in mind that the DAY function only extracts the day part of a date and does not consider the month or year. If you need the complete date or other date components, you may want to use other date functions, such as MONTH, YEAR, or DATE_FORMAT.

Here’s an example of combining DAY(), MONTH(), and YEAR() functions to get the full date:

SELECT event_name,
       DAY(event_date) AS EventDay,
       MONTH(event_date) AS EventMonth,
       YEAR(event_date) AS EventYear
FROM events;

This query retrieves event names along with the day, month, and year of the corresponding event dates.