~drizzle-developers/drizzle/elliott-release

« back to all changes in this revision

Viewing changes to docs/functions/extract_date.rst

  • Committer: Patrick Crews
  • Date: 2011-02-01 20:33:06 UTC
  • mfrom: (1845.2.288 drizzle)
  • Revision ID: gleebix@gmail.com-20110201203306-mwq2rk0it81tlwxh
Merged Trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Extract Functions
 
2
=================
 
3
 
 
4
**Syntax** ::
 
5
 
 
6
        EXTRACT(field FROM source)
 
7
 
 
8
The EXTRACT function retrieves subfields such as day or hour from date/time values. The source value has to be a value expression of type *timestamp*, *time*, *date*, or *interval*. 
 
9
 
 
10
*Field* is an identifier or string that identifies the field to extract from the source value. The extract function returns values of type *double precision*. 
 
11
 
 
12
**Examples:**
 
13
 
 
14
The following field names are available:
 
15
 
 
16
**day** ::
 
17
        
 
18
        SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
 
19
 
 
20
Day of the month.
 
21
 
 
22
*Result: 16*
 
23
 
 
24
**decade** ::
 
25
        
 
26
        SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
 
27
 
 
28
The year, divided by 10.
 
29
 
 
30
*Result: 200*
 
31
 
 
32
**dow** ::
 
33
        
 
34
        SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
 
35
 
 
36
The day of the week (Sunday is 0, Saturday is 6)
 
37
 
 
38
*Result: 5*
 
39
 
 
40
**doy** ::
 
41
 
 
42
        SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
 
43
 
 
44
The day of the year (1 - 365/366)
 
45
 
 
46
*Result: 47*
 
47
 
 
48
**hour** ::
 
49
 
 
50
        SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
 
51
 
 
52
The hour field (0 - 23)
 
53
 
 
54
*Result: 20*
 
55
 
 
56
**microseconds** ::
 
57
 
 
58
        SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
 
59
 
 
60
The seconds field, including fractional parts, multiplied by 1 000 000; note that this includes full seconds
 
61
 
 
62
*Result: 28500000*
 
63
 
 
64
**minute** ::
 
65
 
 
66
        SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
 
67
 
 
68
The minutes field (0 - 59)      
 
69
 
 
70
*Result: 38*
 
71
 
 
72
**month**
 
73
 
 
74
For timestamp values, the number of the month within the year (1 - 12). 
 
75
For interval values, the number of months (0 - 11). ::
 
76
 
 
77
        SELECT EXTRACT(MONTH FROM TIMESTAMP '2010-12-29 08:45:27');
 
78
 
 
79
*Result: 12* ::
 
80
 
 
81
        SELECT EXTRACT(MONTH FROM INTERVAL '3 years 4 months');
 
82
 
 
83
*Result: 4* ::
 
84
 
 
85
        SELECT EXTRACT(MONTH FROM INTERVAL '3 years 13 months');
 
86
 
 
87
*Result: 1*
 
88
 
 
89
**quarter** ::
 
90
 
 
91
        SELECT EXTRACT(QUARTER FROM TIMESTAMP '2010-12-29 08:45:27');
 
92
 
 
93
The quarter of the year (1 - 4) containing the date.
 
94
        
 
95
*Result: 4*
 
96
 
 
97
**second** ::
 
98
 
 
99
        SELECT EXTRACT(SECOND FROM TIMESTAMP '2010-12-29 08:45:27');
 
100
 
 
101
The seconds field, including fractional parts (0 - 59)
 
102
        
 
103
*Result: 27* ::
 
104
 
 
105
        SELECT EXTRACT(SECOND FROM TIME '08:15:22.5');
 
106
 
 
107
*Result: 22.5*
 
108
 
 
109
**timezone**
 
110
 
 
111
The time zone offset from UTC, measured in seconds.
 
112
 
 
113
**week**
 
114
 
 
115
Returns the week number that a day is in. Weeks are numbered according to ISO 8601:1988.
 
116
 
 
117
ISO 8601:1988 means that if the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is the last week of the previous year, and the next week is week 1. The ISO-8601 week starts on Monday.
 
118
 
 
119
It's possible for early January dates to be part of the 52nd or 53rd week of the previous year. For example, 2011-01-01 was part of the 52nd week of year 2010. ::
 
120
 
 
121
        SELECT EXTRACT(WEEK FROM TIMESTAMP '2010-01-25 12:44:06');
 
122
 
 
123
*Result: 4*
 
124
 
 
125
**year** ::
 
126
 
 
127
        SELECT EXTRACT(YEAR FROM TIMESTAMP '2009-02-16 20:38:40');
 
128
 
 
129
*Result: 2009*
 
130
 
 
131
The valid field names for date_part are the same as for extract. ::
 
132
 
 
133
        SELECT date_part('day', TIMESTAMP '2010-07-16 10:12:05');
 
134
 
 
135
*Result: 16* ::
 
136
 
 
137
        SELECT date_part('hour', INTERVAL '5 hours 12 minutes');
 
138
 
 
139
*Result: 4*