6
EXTRACT(field FROM source)
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*.
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*.
14
The following field names are available:
20
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
30
SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
32
The year, divided by 10.
40
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
42
The day of the week (Sunday is 0, Saturday is 6)
50
SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
52
The day of the year (1 - 365/366)
60
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
62
The hour field (0 - 23)
70
SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
72
The seconds field, including fractional parts, multiplied by 1 000 000; note that this includes full seconds
80
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
82
The minutes field (0 - 59)
88
For timestamp values, the number of the month within the year (1 - 12).
89
For interval values, the number of months (0 - 11).
93
SELECT EXTRACT(MONTH FROM TIMESTAMP '2010-12-29 08:45:27');
99
SELECT EXTRACT(MONTH FROM INTERVAL '3 years 4 months');
103
.. code-block:: mysql
105
SELECT EXTRACT(MONTH FROM INTERVAL '3 years 13 months');
111
.. code-block:: mysql
113
SELECT EXTRACT(QUARTER FROM TIMESTAMP '2010-12-29 08:45:27');
115
The quarter of the year (1 - 4) containing the date.
121
.. code-block:: mysql
123
SELECT EXTRACT(SECOND FROM TIMESTAMP '2010-12-29 08:45:27');
125
The seconds field, including fractional parts (0 - 59)
129
.. code-block:: mysql
131
SELECT EXTRACT(SECOND FROM TIME '08:15:22.5');
137
The time zone offset from UTC, measured in seconds.
141
Returns the week number that a day is in. Weeks are numbered according to ISO 8601:1988.
143
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.
145
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.
147
.. code-block:: mysql
149
SELECT EXTRACT(WEEK FROM TIMESTAMP '2010-01-25 12:44:06');
155
.. code-block:: mysql
157
SELECT EXTRACT(YEAR FROM TIMESTAMP '2009-02-16 20:38:40');
161
The valid field names for date_part are the same as for extract.
163
.. code-block:: mysql
165
SELECT date_part('day', TIMESTAMP '2010-07-16 10:12:05');
169
.. code-block:: mysql
171
SELECT date_part('hour', INTERVAL '5 hours 12 minutes');