MySQL DAYOFMONTH() Function

MySQL DAYOFMONTH() function; Through this tutorial, we will learn how to use MySQL DAYOFMONTH() function with the help of examples.

When we need to get a day of the month of the given date in MySQL query that time we could use to MySQL DAYOFMONTH () function. and it will return numeric value. And as well as, we will take some examples with MySQL other functions like, CURDATE(), NOW(), etc.

MySQL DAYOFMONTH() Function

In MySQL, the DAYOFMONTH() is used to return the day of the month from a date. the DAYOFMONTH() function is a synonym for the DAYOFMONTH() function.

The “day of the month” is the value between 1 and 31. For example, if you provide a date of 2020-08-03, then the DAY () function 3 will return.

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.

Examples of MySQL DAYOFMONTH() Function

Example-1

Now we take an example to demonstrate.

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

Output-1

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

Example-2

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 | Database Example

Next database example, we take an example of removing part of the day with a column when running the query against the database.

 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 | Current Date/Time

Let’s take another example , extracting part of the day from the current date and time (which is now returned using the () function).

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

Output-4

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

Example-4 | CURDATE() Function

Let’s take a another example of using CURDATE() function. Basically CURDATE() function returns only the date without time.

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

Output-5

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

Conclusion

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

Recommended MySQL Tutorials

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *