MySQL MONTH Function | MONTH In MySQL In Hindi

📔 : MySQL 🔗

MONTH() function , pass की हुई date के according integer value return करता है जो month को represent करता है। normal syntax कुछ इस तरह से होता है -

MONTH(date_value);

MONTH() function में आप कोई भी date / datetime या कोई भी ऐसी value pass कर सकते हैं , जो date को represent करती हो -

MySQL MONTH Example

mysql> SELECT MONTH('2023-03-016');

Output

+---------------------+
| MONTH('2023-03-16') |
+---------------------+
|                   3 |
+---------------------+

ध्यान रहे अगर आप MONTH() function में '0000-00-00' pass करेंगे तो आपको 0 ही मिलेगा और NULL pass करेंगे तो NULL मिलेगा।

MySQL Get Current Month

तो जैसा कि आप जानते हैं कि NOW() function date और time दोनों return करता है तो , directly हम NOW() function को MONTH() function में pass करके current month get कर सकते हैं।

SELECT MONTH(NOW()) AS current_month;

Output

+---------------+
| current_month |
+---------------+
|             3 |
+---------------+

MySQL Month Function Example

अब कुछ examples देख लेते हैं , जिससे आपको और अच्छे से समझ आ जाये। Suppose, हमारे पास users name की एक table है , जिसमे कुछ इस तरह से records available हैं।

+------------+---------------------+
| name       | created_at          |
+------------+---------------------+
| Tom Cruise | 2023-02-09 13:07:31 |
| John Quill | 2023-02-22 12:07:37 |
| Wick       | 2023-02-08 08:49:42 |
| Rohan      | 2023-01-08 08:49:42 |
+------------+---------------------+

तो मुझे अब वो records fetch करने हैं February month में create हुए हैं , तो उसके लिए कुछ इस तरह से query होगी -

SELECT name , created_at FROM users WHERE MONTH(created_at)=2;

Output

+------------+---------------------+
| name       | created_at          |
+------------+---------------------+
| Tom Cruise | 2023-02-09 13:07:31 |
| John Quill | 2023-02-22 12:07:37 |
| Wick       | 2023-02-08 08:49:42 |
+------------+---------------------+

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers