हिस्सों से तारीख बनाना
अब तक जिन ज़्यादातर फंक्शनों के साथ आपने काम किया है, वे किसी date से हिस्से निकालते हैं, जबकि DATEFROMPARTS() इसका उल्टा करता है: यह तीन संख्याओं (year, month और day) से एक date बनाता है।
सिंटैक्स है:
DATEFROMPARTS(year, month, day)
आप इस फंक्शन के पैरामीटर के रूप में ऐसी expressions भी दे सकते हैं जो संख्यात्मक मान लौटाती हैं, जैसे:
DATEFROMPARTS(YEAR(date_expression), MONTH(date_expression), 2)
इस अभ्यास में, आप voters टेबल से जानकारी चुनेंगे, जिसमें first_vote_date का year और month शामिल होगा। फिर, आप एक नया date कॉलम बनाएँगे जो पहली वोट के month के पहले दिन का प्रतिनिधित्व करेगा.
यह अभ्यास पाठ्यक्रम का हिस्सा है
SQL Server में डेटा मैनिपुलेट करने के लिए फ़ंक्शंस
अभ्यास निर्देश
- पहली वोट का year चुनें।
- पहली वोट की तारीख का month चुनें।
- पहली वोट के month की शुरुआत के रूप में एक date बनाएँ.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
SELECT
first_name,
last_name,
-- Select the year of the first vote
___(first_vote_date) AS first_vote_year,
-- Select the month of the first vote
___(first_vote_date) AS first_vote_month,
-- Create a date as the start of the month of the first vote
___(___, ___, 1) AS first_vote_starting_month
FROM voters;