नीचे दिया गया उदाहरण देखें
SELECT mydate calendar_date
,EXTRACT(WEEK FROM mydate) week_num
,EXTRACT(month FROM mydate) month_num
,to_char(mydate,'Mon') month_name
,EXTRACT(Quarter FROM mydate) quarter_num
,EXTRACT(year FROM mydate) calendar_year
,EXTRACT(DOW FROM mydate) iso_dayofweek
,to_char(mydate, 'day') dayofweek_name
FROM (
SELECT now()::DATE mydate
) t
परिणाम:
calendar_date week_num month_num month_name quarter_num calendar_year iso_dayofweek dayofweek_name
------------- -------- --------- ---------- ----------- ------------- ------------- --------------
2015/04/24 17 4 Apr 2 2015 5 friday
आप उपयोग कर सकते हैं generate_series()
उदाहरण के लिए एक वर्ष में सभी तिथियां प्राप्त करने के लिए:2015
select generate_series(0,364) + date'1/1/2015'
यह 1/1/2015 - 31/12/2015
. से दिनांक प्रस्तुत करेगा , और इसका उपयोग करें चुनें इसके बजाय SELECT now()::DATE
दिए गए उदाहरण में
अगर आप साल के लिए टेबल बनाना चाहते हैं 2015
तो आप निम्न क्वेरी का उपयोग कर सकते हैं
CREATE TABLE mycal_2015 AS
SELECT row_number() OVER () date_key
,mydate calendar_date
,EXTRACT(WEEK FROM mydate) week_num
,EXTRACT(month FROM mydate) month_num
,to_char(mydate,'Mon') month_name
,EXTRACT(Quarter FROM mydate) quarter_num
,EXTRACT(year FROM mydate) calendar_year
,EXTRACT(DOW FROM mydate) iso_dayofweek
,to_char(mydate, 'day') dayofweek_name
FROM (
SELECT generate_series(0, 364) + DATE '1/1/2015' mydate
) t
और तालिका कुछ इस तरह दिखेगी select * from mycal_2015
date_key calendar_date week_num month_num month_name quarter_num calendar_year iso_dayofweek dayofweek_name
-------- ------------- -------- --------- ---------- ----------- ------------- ------------- --------------
1 2015/01/01 1 1 Jan 1 2015 4 thursday
2 2015/01/02 1 1 Jan 1 2015 5 friday
3 2015/01/03 1 1 Jan 1 2015 6 saturday
4 2015/01/04 1 1 Jan 1 2015 0 sunday
5 2015/01/05 2 1 Jan 1 2015 1 monday
6 2015/01/06 2 1 Jan 1 2015 2 tuesday
...
.
.
.
364 2015/12/30 53 12 Dec 4 2015 3 wednesday
365 2015/12/31 53 12 Dec 4 2015 4 thursday
POSTGRESQL:एक्सट्रैक्ट फंक्शन
पोस्टग्रेएसक्यूएल एक्सट्रेक्ट फंक्शन एक तारीख से कुछ हिस्सों को एक्सट्रेक्ट करता है
सिंटैक्स:extract( unit from date )
दिनांक एक दिनांक, टाइमस्टैम्प, समय या अंतराल मान है जिससे दिनांक भाग निकाला जाना है।
इकाई अंतराल का इकाई प्रकार है जैसे दिन, महीना, मिनट, घंटा, और इसी तरह
यह निम्न में से एक हो सकता है:
unit description
--------------- -----------------------------------------------------------------------------------------------------------------------------
century Uses the Gregorian calendar where the first century starts at '0001-01-01 00:00:00 AD'
day Day of the month (1 to 31)
decade Year divided by 10
dow Day of the week (0=Sunday, 1=Monday, 2=Tuesday, ... 6=Saturday)
doy Day of the year (1=first day of year, 365/366=last day of the year, depending if it is a leap year)
epoch Number of seconds since '1970-01-01 00:00:00 UTC', if date value. Number of seconds in an interval, if interval value
hour Hour (0 to 23)
isodow Day of the week (1=Monday, 2=Tuesday, 3=Wednesday, ... 7=Sunday)
isoyear ISO 8601 year value (where the year begins on the Monday of the week that contains January 4th)
microseconds Seconds (and fractional seconds) multiplied by 1,000,000
millennium Millennium value
milliseconds Seconds (and fractional seconds) multiplied by 1,000
minute Minute (0 to 59)
month Number for the month (1 to 12), if date value. Number of months (0 to 11), if interval value
quarter Quarter (1 to 4)
second Seconds (and fractional seconds)
timezone Time zone offset from UTC, expressed in seconds
timezone_hour Hour portion of the time zone offset from UTC
timezone_minute Minute portion of the time zone offset from UTC
week Number of the week of the year based on ISO 8601 (where the year begins on the Monday of the week that contains January 4th)
year Year as 4-digits
नोट:एक्स्ट्रेक्ट फ़ंक्शन PostgreSQL संस्करण 8.4 और इसके बाद के संस्करण पर लागू होता है
दिनांक/समय कार्य और ऑपरेटर
generate_series()