MySQL MONTH Function Examples

MySQL MONTH Function Examples

Get month number from the date in mysql. Through this tutorial, we will learn how to use mysql MONTH() function with syntax and examples. Today we are going to discuss about MySQL MONTH() function with various examples.

When we need to get month of given date in mysql query that time we could use to mysql MONTH() function. and it will return numeric value. We will take some example of mysql month() with various function like, CURDATE(), NOW() etc.

Let’s see, if you give a date of 2019-07-11 in MONTH() function, it will return the 7 month. The MySQL MONTH() function returns only numeric value.

MySQL MONTH() Function

In MySQL, the MONTH() is used to return the month from a date. The MONTH() is return a value between 1 and 12.

The MONTH() is return a value between 1 and 12. For example, if you provide a date of 2020-08-03, then the DAY () function 8 will return.

Syntax

The MONTH() function syntax is:

MONTH(date)

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

Example 1 – get month number from date in mysql

Now we take an example to demonstrate.

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

Output-1

+--------+
| Result |
+--------+
|     8  |
+--------+

Example-2 | get month number from current date in mysql

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

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

Output-2

+--------+
| Result |
+--------+
|      8 |
+--------+

Example-3 | Database Example

For some time we want to fetch a record / data from the mysql database table. When we need to get the name of month a table in the database. In that case we use MONTH() with mysql queries.

Next, we take example-2, in this example we will fetch MONTH of database table name users where column name is created_at.

 SELECT
 created_at AS create_date,
 MONTH(created_at) AS month
 FROM users
 WHERE id= 112;

Output-3

+---------------------+--------------+
| create_date         | month        |
+---------------------+--------------+
| 2010-08-23 10:33:39 |           8  |
+---------------------+--------------+

Example-4 | get month number from the current datetime in mysql

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

  SELECT 
  NOW(),
  MONTH(NOW());

Output-4

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

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(),
MONTH(CURDATE());    

Output-5

+------------+-----------------------+
| CURDATE()  | MONTH(CURDATE())      |
+------------+-----------------------+
| 2019-05-15 |              5        |
+------------+-----------------------+

Conclusion

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

Recommended MySQL Tutorials

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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