← Back to MySQL Course | Chapter 13: Date & Numeric Functions | Lesson 6 of 6

YEAR MONTH DAY

YEAR, MONTH और DAY एक date को टुकड़ों में फाड़ने और सिर्फ year, या month, या day रखने जैसे हैं।
Syntax
sql
YEAR(date)
MONTH(date)
DAY(date)

YEAR Function

YEAR किसी date value से four-digit year निकालता है, जो records को year से group या filter करने का सबसे simple तरीका है, जैसे किसी specific calendar year के लिए sales total करना।

उदाहरण: The YEAR Function

sql
CREATE TABLE orders (id INT, order_date DATE, total INT);
INSERT INTO orders VALUES (1, '2023-05-01', 50), (2, '2024-01-15', 70);
SELECT YEAR(order_date) AS order_year, SUM(total) AS yearly_total FROM orders GROUP BY YEAR(order_date);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

MONTH Function

MONTH किसी date से सिर्फ numeric month extract करता है, जबकि related MONTHNAME function month को text में spelled out return करता है, आपको raw number और एक human-readable label के बीच एक choice देते हुए।

उदाहरण: The MONTH Function

sql
SELECT MONTH('2024-06-15') AS month_number, MONTHNAME('2024-06-15') AS month_text;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

DAY Function

DAY किसी date से day-of-month number return करता है, और DAYOFMONTH एक synonym है जो exact same result return करता है, इसलिए दोनों में से जो नाम आपकी query में ज़्यादा clearly पढ़े काम करता है।

उदाहरण: The DAY Function

sql
SELECT DAY('2024-06-15') AS day_num, DAYOFMONTH('2024-06-15') AS day_num_alias;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Day of the Week और Year

calendar day से आगे, MySQL आपको यह भी बता सकता है कि एक date week के किस दिन या year के किस numbered day पर पड़ती है, इसे एक plain integer की तरह return करते हुए जिसे आप scheduling logic के लिए इस्तेमाल कर सकते हैं।

उदाहरण: Day of the Week and Year

sql
SELECT DAYOFWEEK('2024-06-15') AS weekday_num, DAYOFYEAR('2024-06-15') AS year_day_num;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Table Records Filter करना

ये extraction functions खासतौर पर एक WHERE clause के अंदर उपयोगी हैं, आपको एक exact date range match किए बिना table को सिर्फ किसी particular year या month की rows तक filter करने देते हुए।

उदाहरण: Filtering Table Records

sql
CREATE TABLE orders (id INT, order_date DATE);
INSERT INTO orders VALUES (1, '2024-03-10'), (2, '2023-03-10');
SELECT * FROM orders WHERE YEAR(order_date) = 2024;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक बड़े indexed column पर YEAR(order_date) = 2024 इस्तेमाल करना और index इस्तेमाल होने से रोकना।
  2. MONTH (number) को MONTHNAME (text) से confuse करना।
  3. एक non-date format में text की तरह stored value पर DAY call करना, जो NULL return करता है।
चैप्टर सारांश
  • ROUND, CEIL, FLOOR, ABS, और MOD numbers के साथ काम करते हैं।
  • NOW, CURDATE, और CURTIME current date और time return करते हैं।
  • DATE_FORMAT, DATEDIFF, DATE_ADD, और YEAR, MONTH, DAY dates के साथ format और calculate करते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.