MySQL DAY() Function

MySQL DAY() function; Through this tutorial, i am going to show you how to find or get the day of a month for a given date from range 0 to 31 using MySQL day() function.

MySQL DAY() Function

MySQL Day() function is one of the MySQL Date Functions, which is  used to return the day of a month for a given date range from 0 to 31.

Syntax of MySQL DAY() Function

The DAY() function syntax is:

DAY(date)

The date here is the date value that you want the day of the month from which you returned.

Example 1 – MySQL DAY() Function

See the first example of mysql day() function; as follows:

SELECT DAY('2020-06-18') AS 'Result';

Output-1

+--------+
| Result |
+--------+
|     18 |
+--------+

Example 2 – MySQL DAY() Function

If there is a leading zero in the part of the day, then the leading zero has been left out of the result.

SELECT DAY('2018-02-01') AS 'Result';

Output-2

+--------+
| Result |
+--------+
|      1 |
+--------+

Example 3 – MySQL DAY() Function

Take an example of removing part of the day with a column when running the query against the database; as follows:

SELECT
  created_at AS created_date,
  DAY(created_at) AS day_of_month
FROM users
WHERE where= 1;

Output-3

+---------------------+--------------+
| created date        | day_of_month |
+---------------------+--------------+
| 2010-08-23 10:33:39 |           23 |
+---------------------+--------------+

Example 4 – MySQL DAY() Function

To extract part of the day from the current date and time (which is now returned using the () function); as follows:

  SELECT 
  NOW(),
  DAY(NOW());

Output-4

+---------------------+------------+
| NOW()               | DAY(NOW()) |
+---------------------+------------+
| 2019-07-10 18:30:44 |         10 |
+---------------------+------------+

Example 5 – MySQL DAY() Function

Let’s take an example using mysql day and curdate() function; as follows:

SELECT 
CURDATE(),
DAY(CURDATE());    

Output-5

+------------+----------------+
| CURDATE()  | DAY(CURDATE()) |
+------------+----------------+
| 2019-05-15 |             15 |
+------------+----------------+

Conclusion

MySQL day() function; In this tutorial, we have leaned how to use mysql day() function with curdate(), now(), etc function to get the day of month.

Leave a Comment