1994.4.87
by Marisa Plumb
adding to date functions |
1 |
EXTRACT DATE FUNCTION
|
2 |
======================
|
|
3 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
4 |
Syntax
|
5 |
------
|
|
6 |
||
7 |
.. code-block:: mysql |
|
1994.4.87
by Marisa Plumb
adding to date functions |
8 |
|
9 |
EXTRACT(field FROM source) |
|
10 |
||
11 |
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*. |
|
12 |
||
13 |
*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*. |
|
14 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
15 |
Examples
|
16 |
--------
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
17 |
|
18 |
The following field names are available: |
|
19 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
20 |
day
|
21 |
^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
22 |
|
23 |
.. code-block:: mysql |
|
24 |
||
1994.4.93
by Marisa Plumb
additions, edits |
25 |
SELECT EXTRACT(DAY FROM TIMESTAMP '2011-02-16 20:38:40'); |
1994.4.87
by Marisa Plumb
adding to date functions |
26 |
|
27 |
Day of the month. |
|
28 |
||
29 |
*Result: 16*
|
|
30 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
31 |
decade
|
32 |
^^^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
33 |
|
34 |
.. code-block:: mysql |
|
35 |
||
36 |
SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40'); |
|
37 |
||
38 |
The year, divided by 10. |
|
39 |
||
40 |
*Result: 200*
|
|
41 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
42 |
dow
|
43 |
^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
44 |
|
45 |
.. code-block:: mysql |
|
46 |
||
47 |
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); |
|
48 |
||
49 |
The day of the week (Sunday is 0, Saturday is 6) |
|
50 |
||
51 |
*Result: 5*
|
|
52 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
53 |
doy
|
54 |
^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
55 |
|
56 |
.. code-block:: mysql |
|
57 |
||
58 |
SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40'); |
|
59 |
||
60 |
The day of the year (1 - 365/366) |
|
61 |
||
62 |
*Result: 47*
|
|
63 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
64 |
hour
|
65 |
^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
66 |
|
67 |
.. code-block:: mysql |
|
68 |
||
69 |
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40'); |
|
70 |
||
71 |
The hour field (0 - 23) |
|
72 |
||
73 |
*Result: 20*
|
|
74 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
75 |
microseconds
|
76 |
^^^^^^^^^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
77 |
|
78 |
.. code-block:: mysql |
|
79 |
||
80 |
SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5'); |
|
81 |
||
82 |
The seconds field, including fractional parts, multiplied by 1 000 000; note that this includes full seconds |
|
83 |
||
84 |
*Result: 28500000*
|
|
85 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
86 |
minute
|
87 |
^^^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
88 |
|
89 |
.. code-block:: mysql |
|
90 |
||
91 |
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40'); |
|
92 |
||
93 |
The minutes field (0 - 59) |
|
94 |
||
95 |
*Result: 38*
|
|
96 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
97 |
month
|
98 |
^^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
99 |
|
100 |
For timestamp values, the number of the month within the year (1 - 12). |
|
101 |
For interval values, the number of months (0 - 11). |
|
102 |
||
103 |
.. code-block:: mysql |
|
104 |
||
105 |
SELECT EXTRACT(MONTH FROM TIMESTAMP '2010-12-29 08:45:27'); |
|
106 |
||
107 |
*Result: 12*
|
|
108 |
||
109 |
.. code-block:: mysql |
|
110 |
||
111 |
SELECT EXTRACT(MONTH FROM INTERVAL '3 years 4 months'); |
|
112 |
||
113 |
*Result: 4*
|
|
114 |
||
115 |
.. code-block:: mysql |
|
116 |
||
117 |
SELECT EXTRACT(MONTH FROM INTERVAL '3 years 13 months'); |
|
118 |
||
119 |
*Result: 1*
|
|
120 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
121 |
quarter
|
122 |
^^^^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
123 |
|
124 |
.. code-block:: mysql |
|
125 |
||
126 |
SELECT EXTRACT(QUARTER FROM TIMESTAMP '2010-12-29 08:45:27'); |
|
127 |
||
128 |
The quarter of the year (1 - 4) containing the date. |
|
129 |
||
130 |
*Result: 4*
|
|
131 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
132 |
second
|
133 |
^^^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
134 |
|
135 |
.. code-block:: mysql |
|
136 |
||
137 |
SELECT EXTRACT(SECOND FROM TIMESTAMP '2010-12-29 08:45:27'); |
|
138 |
||
139 |
The seconds field, including fractional parts (0 - 59) |
|
140 |
||
141 |
*Result: 27*
|
|
142 |
||
143 |
.. code-block:: mysql |
|
144 |
||
145 |
SELECT EXTRACT(SECOND FROM TIME '08:15:22.5'); |
|
146 |
||
147 |
*Result: 22.5*
|
|
148 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
149 |
timezone
|
150 |
^^^^^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
151 |
|
152 |
The time zone offset from UTC, measured in seconds. |
|
153 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
154 |
week
|
155 |
^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
156 |
|
157 |
Returns the week number that a day is in. Weeks are numbered according to ISO 8601:1988. |
|
158 |
||
159 |
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. |
|
160 |
||
161 |
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. |
|
162 |
||
163 |
.. code-block:: mysql |
|
164 |
||
165 |
SELECT EXTRACT(WEEK FROM TIMESTAMP '2010-01-25 12:44:06'); |
|
166 |
||
167 |
*Result: 4*
|
|
168 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
169 |
year
|
170 |
^^^^
|
|
1994.4.87
by Marisa Plumb
adding to date functions |
171 |
|
172 |
.. code-block:: mysql |
|
173 |
||
174 |
SELECT EXTRACT(YEAR FROM TIMESTAMP '2009-02-16 20:38:40'); |
|
175 |
||
176 |
*Result: 2009*
|
|
177 |
||
2194.5.3
by Andrew Hutchings
Markup fixes |
178 |
date_part
|
179 |
^^^^^^^^^
|
|
180 |
||
1994.4.87
by Marisa Plumb
adding to date functions |
181 |
The valid field names for date_part are the same as for extract. |
182 |
||
183 |
.. code-block:: mysql |
|
184 |
||
185 |
SELECT date_part('day', TIMESTAMP '2010-07-16 10:12:05'); |
|
186 |
||
187 |
*Result: 16*
|
|
188 |
||
189 |
.. code-block:: mysql |
|
190 |
||
191 |
SELECT date_part('hour', INTERVAL '5 hours 12 minutes'); |
|
192 |
||
193 |
*Result: 4*
|