अग्रणी शून्य का उपयोग करने के लिए mySQL MONTH () प्राप्त करें?


91

मैं इस क्वेरी में 8 के बजाय '08' को वापस करने के लिए mySQL के MONTH () फ़ंक्शन को कैसे निर्दिष्ट करूँ?

मुझे डेटवाइज काम करना पसंद है। वर्तमान में जैसे तारीख के लिए परिणाम हो रही है

2006-9
2007-1
2007-10
2007-11

वर्तमान क्वेरी:

SELECT COUNT(*), CONCAT(YEAR(`datetime_added`), '-', MONTH(`datetime_added`)) as date FROM `person` WHERE (email = '' OR email IS NULL) 
GROUP BY date 
ORDER BY date ASC

जवाबों:


204

इसके बजाय निम्नलिखित का उपयोग करें:

DATE_FORMAT(`datetime_added`,'%Y-%m')

स्पष्टीकरण:

DATE_FORMAT()फ़ंक्शन आपको नीचे दी गई तालिका में वर्णित विशेषकों का उपयोग करते हुए वैसे भी तारीख को प्रारूपित करने देता है ( प्रलेखन से शब्दशः लिया गया है )। तो एक प्रारूप स्ट्रिंग का '%Y-%m'अर्थ है: "एक पूर्ण वर्ष (4 अंक), एक डैश ( -), उसके बाद दो अंकों वाला महीना नंबर"।

ध्यान दें कि आप lc_time_namesसिस्टम चर सेट करके दिन / महीने के नाम के लिए उपयोग की जाने वाली भाषा को निर्दिष्ट कर सकते हैं । अत्यंत उपयोगी। अधिक जानकारी के लिए प्रलेखन देखें।

Specifier   Description
%a  Abbreviated weekday name (Sun..Sat)
%b  Abbreviated month name (Jan..Dec)
%c  Month, numeric (0..12)
%D  Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d  Day of the month, numeric (00..31)
%e  Day of the month, numeric (0..31)
%f  Microseconds (000000..999999)
%H  Hour (00..23)
%h  Hour (01..12)
%I  Hour (01..12)
%i  Minutes, numeric (00..59)
%j  Day of year (001..366)
%k  Hour (0..23)
%l  Hour (1..12)
%M  Month name (January..December)
%m  Month, numeric (00..12)
%p  AM or PM
%r  Time, 12-hour (hh:mm:ss followed by AM or PM)
%S  Seconds (00..59)
%s  Seconds (00..59)
%T  Time, 24-hour (hh:mm:ss)
%U  Week (00..53), where Sunday is the first day of the week
%u  Week (00..53), where Monday is the first day of the week
%V  Week (01..53), where Sunday is the first day of the week; used with %X
%v  Week (01..53), where Monday is the first day of the week; used with %x
%W  Weekday name (Sunday..Saturday)
%w  Day of the week (0=Sunday..6=Saturday)
%X  Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x  Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y  Year, numeric, four digits
%y  Year, numeric (two digits)
%%  A literal “%” character
%x  x, for any x not listed above 

8
जबकि उसने जो नहीं पूछा था, यह जवाब देने के लिए लगता है कि उसे क्या पूछना चाहिए था।
जेरेमी होलोवक्स

महीने का दिन 0 क्यों हो सकता है?
14

क्योंकि 0000-00-00 एक वैध तिथि है (सेटिंग्स के आधार पर)
14

2
@SOFe> महीने और दिन के निर्दिष्ट के लिए रंग इस तथ्य के कारण शून्य से शुरू होते हैं कि MySQL अधूरी तारीखों जैसे '2014-00-00' के भंडारण की अनुमति देता है। dev.mysql.com/doc/refman/5.6/en/…
kio21

बहुत बढ़िया, एक आकर्षण की तरह काम किया :)
मिजो गेम्स

31

आप जैसे पेडिंग का उपयोग कर सकते हैं

SELECT
    COUNT(*), 
    CONCAT(YEAR(`datetime_added`), '-', LPAD(MONTH(`datetime_added`), 2, '0')) as date 
FROM `person` 
WHERE (email = '' OR email IS NULL) 
GROUP BY date 
ORDER BY date ASC

हाइव में जरूरत को पूरा किया। DATE_FORMAT () फ़ंक्शन हाइव में समर्थित नहीं है। आपका जवाब मदद करता है।
गुआंगटोंग शेन


4

MONTH () पूर्णांक देता है, इसलिए निश्चित रूप से कोई अग्रणी शून्य नहीं है। आपको इसे एक स्ट्रिंग में बदलना होगा, बाएं-पैड को '0' और अंतिम 2 अक्षरों को लेना होगा।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.