MySQL DAYOFMONTH() Function

MySQL DAYOFMONTH() function; Through this tutorial, i am going to show you how to get day of month using the MySQL DAYOFMONTH() function with the help of examples.

MySQL DAYOFMONTH() Function

MySQL DAYOFMONTH() returns the day of the month for a given date. The day returned will be within the range of 1 to 31. If the date is ‘0000-00-00’, the function will return 0. The DAY() is the synonym of DAYOFMONTH().

Syntax of MySQL DAYOFMONTH() Function

The DAYOFMONTH() function syntax is:

DAYOFMONTH(date)

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

Example 1 – MySQL DAYOFMONTH() Function

Now i will take first example for dayofmonth(); as follows:

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

Output-1

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

Example 2 – MySQL DAYOFMONTH() 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 DAYOFMONTH('2018-02-01') AS 'Result';

Output-2

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

Example 3 – MySQL DAYOFMONTH() 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 create_date,
 DAYOFMONTH(created_at) AS day_of_month
 FROM users
 WHERE id= 112;

Output-3

+---------------------+--------------+
| create_date         | day_of_month |
+---------------------+--------------+
| 2010-08-23 10:33:39 |           23 |
+---------------------+--------------+

Example 4 – MySQL DAYOFMONTH() with Now() Function

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

  SELECT 
  NOW(),
  DAYOFMONTH(NOW());

Output-4

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

Example 5- MySQL DAYOFMONTH() Function

Example of Dayofmonth() with CURDATE() function; as follows:

SELECT 
CURDATE(),
DAYOFMONTH(CURDATE());    

Output-5

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

Conclusion

Through this tutorial, You have learned how to use mysql DAYOFMONTH() function with various examples.

Leave a Comment