YEAR MONTH DAY
In this page:
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
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);
MONTH Function
MONTH किसी date से सिर्फ numeric month extract करता है, जबकि related MONTHNAME function month को text में spelled out return करता है, आपको raw number और एक human-readable label के बीच एक choice देते हुए।
उदाहरण: The MONTH Function
SELECT MONTH('2024-06-15') AS month_number, MONTHNAME('2024-06-15') AS month_text;
DAY Function
DAY किसी date से day-of-month number return करता है, और DAYOFMONTH एक synonym है जो exact same result return करता है, इसलिए दोनों में से जो नाम आपकी query में ज़्यादा clearly पढ़े काम करता है।
उदाहरण: The DAY Function
SELECT DAY('2024-06-15') AS day_num, DAYOFMONTH('2024-06-15') AS day_num_alias;
Day of the Week और Year
calendar day से आगे, MySQL आपको यह भी बता सकता है कि एक date week के किस दिन या year के किस numbered day पर पड़ती है, इसे एक plain integer की तरह return करते हुए जिसे आप scheduling logic के लिए इस्तेमाल कर सकते हैं।
उदाहरण: Day of the Week and Year
SELECT DAYOFWEEK('2024-06-15') AS weekday_num, DAYOFYEAR('2024-06-15') AS year_day_num;
Table Records Filter करना
ये extraction functions खासतौर पर एक WHERE clause के अंदर उपयोगी हैं, आपको एक exact date range match किए बिना table को सिर्फ किसी particular year या month की rows तक filter करने देते हुए।
उदाहरण: Filtering Table Records
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;
- एक बड़े indexed column पर
YEAR(order_date) = 2024इस्तेमाल करना और index इस्तेमाल होने से रोकना। MONTH(number) कोMONTHNAME(text) से confuse करना।- एक non-date format में text की तरह stored value पर
DAYcall करना, जोNULLreturn करता है।
ROUND,CEIL,FLOOR,ABS, औरMODnumbers के साथ काम करते हैं।NOW,CURDATE, औरCURTIMEcurrent date और time return करते हैं।DATE_FORMAT,DATEDIFF,DATE_ADD, औरYEAR,MONTH,DAYdates के साथ format और calculate करते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: