@@ -9077,7 +9077,8 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
9077
9077
</para>
9078
9078
<para>
9079
9079
Subtract timestamps (converting 24-hour intervals into days,
9080
- similarly to <function>justify_hours()</function>)
9080
+ similarly to <link
9081
+ linkend="function-justify-hours"><function>justify_hours()</function></link>)
9081
9082
</para>
9082
9083
<para>
9083
9084
<literal>timestamp '2001-09-29 03:00' - timestamp '2001-07-27 12:00'</literal>
@@ -9490,35 +9491,35 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
9490
9491
9491
9492
<row>
9492
9493
<entry role="func_table_entry"><para role="func_signature">
9493
- <indexterm>
9494
+ <indexterm id="function-justify-days" >
9494
9495
<primary>justify_days</primary>
9495
9496
</indexterm>
9496
9497
<function>justify_days</function> ( <type>interval</type> )
9497
9498
<returnvalue>interval</returnvalue>
9498
9499
</para>
9499
9500
<para>
9500
- Adjust interval so 30-day time periods are represented as months
9501
+ Adjust interval, converting 30-day time periods to months
9501
9502
</para>
9502
9503
<para>
9503
- <literal>justify_days(interval '35 days')</literal>
9504
- <returnvalue>1 mon 5 days</returnvalue>
9504
+ <literal>justify_days(interval '1 year 65 days')</literal>
9505
+ <returnvalue>1 year 2 mons 5 days</returnvalue>
9505
9506
</para></entry>
9506
9507
</row>
9507
9508
9508
9509
<row>
9509
9510
<entry role="func_table_entry"><para role="func_signature">
9510
- <indexterm>
9511
+ <indexterm id="function-justify-hours" >
9511
9512
<primary>justify_hours</primary>
9512
9513
</indexterm>
9513
9514
<function>justify_hours</function> ( <type>interval</type> )
9514
9515
<returnvalue>interval</returnvalue>
9515
9516
</para>
9516
9517
<para>
9517
- Adjust interval so 24-hour time periods are represented as days
9518
+ Adjust interval, converting 24-hour time periods to days
9518
9519
</para>
9519
9520
<para>
9520
- <literal>justify_hours(interval '27 hours')</literal>
9521
- <returnvalue>1 day 03:00 :00</returnvalue>
9521
+ <literal>justify_hours(interval '50 hours 10 minutes ')</literal>
9522
+ <returnvalue>2 days 02:10 :00</returnvalue>
9522
9523
</para></entry>
9523
9524
</row>
9524
9525
@@ -9951,13 +9952,19 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9951
9952
The <function>extract</function> function retrieves subfields
9952
9953
such as year or hour from date/time values.
9953
9954
<replaceable>source</replaceable> must be a value expression of
9954
- type <type>timestamp</type>, <type>time </type>, or <type>interval </type>.
9955
- (Expressions of type <type>date </type> are
9956
- cast to <type>timestamp</type> and can therefore be used as
9957
- well.) <replaceable>field</replaceable> is an identifier or
9955
+ type <type>timestamp</type>, <type>date </type>, <type>time </type>,
9956
+ or <type>interval </type>. (Timestamps and times can be with or
9957
+ without time zone.)
9958
+ <replaceable>field</replaceable> is an identifier or
9958
9959
string that selects what field to extract from the source value.
9960
+ Not all fields are valid for every input data type; for example, fields
9961
+ smaller than a day cannot be extracted from a <type>date</type>, while
9962
+ fields of a day or more cannot be extracted from a <type>time</type>.
9959
9963
The <function>extract</function> function returns values of type
9960
9964
<type>numeric</type>.
9965
+ </para>
9966
+
9967
+ <para>
9961
9968
The following are valid field names:
9962
9969
9963
9970
<!-- alphabetical -->
@@ -9966,40 +9973,36 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9966
9973
<term><literal>century</literal></term>
9967
9974
<listitem>
9968
9975
<para>
9969
- The century
9976
+ The century; for <type>interval</type> values, the year field
9977
+ divided by 100
9970
9978
</para>
9971
9979
9972
9980
<screen>
9973
9981
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
9974
9982
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9975
9983
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
9976
9984
<lineannotation>Result: </lineannotation><computeroutput>21</computeroutput>
9985
+ SELECT EXTRACT(CENTURY FROM DATE '0001-01-01 AD');
9986
+ <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
9987
+ SELECT EXTRACT(CENTURY FROM DATE '0001-12-31 BC');
9988
+ <lineannotation>Result: </lineannotation><computeroutput>-1</computeroutput>
9989
+ SELECT EXTRACT(CENTURY FROM INTERVAL '2001 years');
9990
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9977
9991
</screen>
9978
-
9979
- <para>
9980
- The first century starts at 0001-01-01 00:00:00 AD, although
9981
- they did not know it at the time. This definition applies to all
9982
- Gregorian calendar countries. There is no century number 0,
9983
- you go from -1 century to 1 century.
9984
-
9985
- If you disagree with this, please write your complaint to:
9986
- Pope, Cathedral Saint-Peter of Roma, Vatican.
9987
- </para>
9988
9992
</listitem>
9989
9993
</varlistentry>
9990
9994
9991
9995
<varlistentry>
9992
9996
<term><literal>day</literal></term>
9993
9997
<listitem>
9994
9998
<para>
9995
- For <type>timestamp</type> values, the day ( of the month) field
9996
- (1–31) ; for <type>interval</type> values, the number of days
9999
+ The day of the month (1–31); for <type>interval</type>
10000
+ values, the number of days
9997
10001
</para>
9998
10002
9999
10003
<screen>
10000
10004
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
10001
10005
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
10002
-
10003
10006
SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
10004
10007
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
10005
10008
</screen>
@@ -10073,10 +10076,8 @@ SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
10073
10076
<screen>
10074
10077
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
10075
10078
<lineannotation>Result: </lineannotation><computeroutput>982384720.120000</computeroutput>
10076
-
10077
10079
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40.12');
10078
10080
<lineannotation>Result: </lineannotation><computeroutput>982355920.120000</computeroutput>
10079
-
10080
10081
SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
10081
10082
<lineannotation>Result: </lineannotation><computeroutput>442800.000000</computeroutput>
10082
10083
</screen>
@@ -10104,7 +10105,8 @@ SELECT to_timestamp(982384720.12);
10104
10105
<term><literal>hour</literal></term>
10105
10106
<listitem>
10106
10107
<para>
10107
- The hour field (0–23)
10108
+ The hour field (0–23 in timestamps, unrestricted in
10109
+ intervals)
10108
10110
</para>
10109
10111
10110
10112
<screen>
@@ -10139,7 +10141,7 @@ SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
10139
10141
<listitem>
10140
10142
<para>
10141
10143
The <acronym>ISO</acronym> 8601 week-numbering year that the date
10142
- falls in (not applicable to intervals)
10144
+ falls in
10143
10145
</para>
10144
10146
10145
10147
<screen>
@@ -10156,9 +10158,6 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
10156
10158
different from the Gregorian year. See the <literal>week</literal>
10157
10159
field for more information.
10158
10160
</para>
10159
- <para>
10160
- This field is not available in PostgreSQL releases prior to 8.3.
10161
- </para>
10162
10161
</listitem>
10163
10162
</varlistentry>
10164
10163
@@ -10167,7 +10166,7 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
10167
10166
<listitem>
10168
10167
<para>
10169
10168
The <firstterm>Julian Date</firstterm> corresponding to the
10170
- date or timestamp (not applicable to intervals) . Timestamps
10169
+ date or timestamp. Timestamps
10171
10170
that are not local midnight result in a fractional value. See
10172
10171
<xref linkend="datetime-julian-dates"/> for more information.
10173
10172
</para>
@@ -10200,12 +10199,15 @@ SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
10200
10199
<term><literal>millennium</literal></term>
10201
10200
<listitem>
10202
10201
<para>
10203
- The millennium
10202
+ The millennium; for <type>interval</type> values, the year field
10203
+ divided by 1000
10204
10204
</para>
10205
10205
10206
10206
<screen>
10207
10207
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
10208
10208
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
10209
+ SELECT EXTRACT(MILLENNIUM FROM INTERVAL '2001 years');
10210
+ <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
10209
10211
</screen>
10210
10212
10211
10213
<para>
@@ -10248,18 +10250,16 @@ SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
10248
10250
<term><literal>month</literal></term>
10249
10251
<listitem>
10250
10252
<para>
10251
- For <type>timestamp</type> values, the number of the month
10252
- within the year (1–12) ; for <type>interval</type> values,
10253
- the number of months, modulo 12 (0–11)
10253
+ The number of the month within the year (1–12);
10254
+ for <type>interval</type> values, the number of months modulo 12
10255
+ (0–11)
10254
10256
</para>
10255
10257
10256
10258
<screen>
10257
10259
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
10258
10260
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
10259
-
10260
10261
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
10261
10262
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
10262
-
10263
10263
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
10264
10264
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
10265
10265
</screen>
@@ -10290,7 +10290,6 @@ SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
10290
10290
<screen>
10291
10291
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
10292
10292
<lineannotation>Result: </lineannotation><computeroutput>40.000000</computeroutput>
10293
-
10294
10293
SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
10295
10294
<lineannotation>Result: </lineannotation><computeroutput>28.500000</computeroutput>
10296
10295
</screen>
@@ -10372,6 +10371,20 @@ SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
10372
10371
</variablelist>
10373
10372
</para>
10374
10373
10374
+ <para>
10375
+ When processing an <type>interval</type> value,
10376
+ the <function>extract</function> function produces field values that
10377
+ match the interpretation used by the interval output function. This
10378
+ can produce surprising results if one starts with a non-normalized
10379
+ interval representation, for example:
10380
+ <screen>
10381
+ SELECT INTERVAL '80 minutes';
10382
+ <lineannotation>Result: </lineannotation><computeroutput>01:20:00</computeroutput>
10383
+ SELECT EXTRACT(MINUTES FROM INTERVAL '80 minutes');
10384
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
10385
+ </screen>
10386
+ </para>
10387
+
10375
10388
<note>
10376
10389
<para>
10377
10390
When the input value is +/-Infinity, <function>extract</function> returns
@@ -10409,7 +10422,6 @@ date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
10409
10422
<screen>
10410
10423
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
10411
10424
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
10412
-
10413
10425
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
10414
10426
<lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
10415
10427
</screen>
@@ -10487,16 +10499,12 @@ date_trunc(<replaceable>field</replaceable>, <replaceable>source</replaceable> [
10487
10499
<screen>
10488
10500
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
10489
10501
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00</computeroutput>
10490
-
10491
10502
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
10492
10503
<lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00</computeroutput>
10493
-
10494
10504
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
10495
10505
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 00:00:00-05</computeroutput>
10496
-
10497
10506
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
10498
10507
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 08:00:00-05</computeroutput>
10499
-
10500
10508
SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
10501
10509
<lineannotation>Result: </lineannotation><computeroutput>3 days 02:00:00</computeroutput>
10502
10510
</screen>
@@ -10535,7 +10543,6 @@ date_bin(<replaceable>stride</replaceable>, <replaceable>source</replaceable>, <
10535
10543
<screen>
10536
10544
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01');
10537
10545
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:30:00</computeroutput>
10538
-
10539
10546
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30');
10540
10547
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:32:30</computeroutput>
10541
10548
</screen>
@@ -10659,10 +10666,8 @@ SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-0
10659
10666
<screen>
10660
10667
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'America/Denver';
10661
10668
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 19:38:40-08</computeroutput>
10662
-
10663
10669
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'America/Denver';
10664
10670
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 18:38:40</computeroutput>
10665
-
10666
10671
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'Asia/Tokyo' AT TIME ZONE 'America/Chicago';
10667
10672
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 05:38:40</computeroutput>
10668
10673
</screen>
@@ -10737,16 +10742,12 @@ LOCALTIMESTAMP(<replaceable>precision</replaceable>)
10737
10742
<screen>
10738
10743
SELECT CURRENT_TIME;
10739
10744
<lineannotation>Result: </lineannotation><computeroutput>14:39:53.662522-05</computeroutput>
10740
-
10741
10745
SELECT CURRENT_DATE;
10742
10746
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23</computeroutput>
10743
-
10744
10747
SELECT CURRENT_TIMESTAMP;
10745
10748
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522-05</computeroutput>
10746
-
10747
10749
SELECT CURRENT_TIMESTAMP(2);
10748
10750
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.66-05</computeroutput>
10749
-
10750
10751
SELECT LOCALTIMESTAMP;
10751
10752
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522</computeroutput>
10752
10753
</screen>
0 commit comments