@@ -8606,7 +8606,8 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
8606
8606
</para>
8607
8607
<para>
8608
8608
Subtract timestamps (converting 24-hour intervals into days,
8609
- similarly to <function>justify_hours()</function>)
8609
+ similarly to <link
8610
+ linkend="function-justify-hours"><function>justify_hours()</function></link>)
8610
8611
</para>
8611
8612
<para>
8612
8613
<literal>timestamp '2001-09-29 03:00' - timestamp '2001-07-27 12:00'</literal>
@@ -8975,35 +8976,35 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
8975
8976
8976
8977
<row>
8977
8978
<entry role="func_table_entry"><para role="func_signature">
8978
- <indexterm>
8979
+ <indexterm id="function-justify-days" >
8979
8980
<primary>justify_days</primary>
8980
8981
</indexterm>
8981
8982
<function>justify_days</function> ( <type>interval</type> )
8982
8983
<returnvalue>interval</returnvalue>
8983
8984
</para>
8984
8985
<para>
8985
- Adjust interval so 30-day time periodsare represented as months
8986
+ Adjust interval, converting 30-day time periodsto months
8986
8987
</para>
8987
8988
<para>
8988
- <literal>justify_days(interval '35 days')</literal>
8989
- <returnvalue>1mon 5 days</returnvalue>
8989
+ <literal>justify_days(interval '1 year 65 days')</literal>
8990
+ <returnvalue>1year 2 mons 5 days</returnvalue>
8990
8991
</para></entry>
8991
8992
</row>
8992
8993
8993
8994
<row>
8994
8995
<entry role="func_table_entry"><para role="func_signature">
8995
- <indexterm>
8996
+ <indexterm id="function-justify-hours" >
8996
8997
<primary>justify_hours</primary>
8997
8998
</indexterm>
8998
8999
<function>justify_hours</function> ( <type>interval</type> )
8999
9000
<returnvalue>interval</returnvalue>
9000
9001
</para>
9001
9002
<para>
9002
- Adjust interval so 24-hour time periodsare represented as days
9003
+ Adjust interval, converting 24-hour time periodsto days
9003
9004
</para>
9004
9005
<para>
9005
- <literal>justify_hours(interval '27 hours')</literal>
9006
- <returnvalue>1 day 03:00 :00</returnvalue>
9006
+ <literal>justify_hours(interval '50 hours 10 minutes ')</literal>
9007
+ <returnvalue>2 days 02:10 :00</returnvalue>
9007
9008
</para></entry>
9008
9009
</row>
9009
9010
@@ -9426,13 +9427,19 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9426
9427
The <function>extract</function> function retrieves subfields
9427
9428
such as year or hour from date/time values.
9428
9429
<replaceable>source</replaceable> must be a value expression of
9429
- type <type>timestamp</type>, <type>time </type>,or <type>interval </type>.
9430
- (Expressions of type <type>date </type> are
9431
- cast to <type>timestamp</type> and can therefore be used as
9432
- well.) <replaceable>field</replaceable> is an identifier or
9430
+ type <type>timestamp</type>, <type>date </type>, <type>time </type>,
9431
+ or <type>interval </type>. (Timestamps and times can be with or
9432
+ without time zone.)
9433
+ <replaceable>field</replaceable> is an identifier or
9433
9434
string that selects what field to extract from the source value.
9435
+ Not all fields are valid for every input data type; for example, fields
9436
+ smaller than a day cannot be extracted from a <type>date</type>, while
9437
+ fields of a day or more cannot be extracted from a <type>time</type>.
9434
9438
The <function>extract</function> function returns values of type
9435
9439
<type>numeric</type>.
9440
+ </para>
9441
+
9442
+ <para>
9436
9443
The following are valid field names:
9437
9444
9438
9445
<!-- alphabetical -->
@@ -9441,40 +9448,36 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9441
9448
<term><literal>century</literal></term>
9442
9449
<listitem>
9443
9450
<para>
9444
- The century
9451
+ The century; for <type>interval</type> values, the year field
9452
+ divided by 100
9445
9453
</para>
9446
9454
9447
9455
<screen>
9448
9456
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
9449
9457
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9450
9458
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
9451
9459
<lineannotation>Result: </lineannotation><computeroutput>21</computeroutput>
9460
+ SELECT EXTRACT(CENTURY FROM DATE '0001-01-01 AD');
9461
+ <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
9462
+ SELECT EXTRACT(CENTURY FROM DATE '0001-12-31 BC');
9463
+ <lineannotation>Result: </lineannotation><computeroutput>-1</computeroutput>
9464
+ SELECT EXTRACT(CENTURY FROM INTERVAL '2001 years');
9465
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9452
9466
</screen>
9453
-
9454
- <para>
9455
- The first century starts at 0001-01-01 00:00:00 AD, although
9456
- they did not know it at the time. This definition applies to all
9457
- Gregorian calendar countries. There is no century number 0,
9458
- you go from -1 century to 1 century.
9459
-
9460
- If you disagree with this, please write your complaint to:
9461
- Pope, Cathedral Saint-Peter of Roma, Vatican.
9462
- </para>
9463
9467
</listitem>
9464
9468
</varlistentry>
9465
9469
9466
9470
<varlistentry>
9467
9471
<term><literal>day</literal></term>
9468
9472
<listitem>
9469
9473
<para>
9470
- For <type>timestamp</type> values, the day( of the month) field
9471
- (1–31) ; for <type>interval</type> values, the number of days
9474
+ The day of the month (1–31); for <type>interval</type>
9475
+ values, the number of days
9472
9476
</para>
9473
9477
9474
9478
<screen>
9475
9479
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
9476
9480
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
9477
-
9478
9481
SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
9479
9482
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
9480
9483
</screen>
@@ -9548,10 +9551,8 @@ SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
9548
9551
<screen>
9549
9552
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
9550
9553
<lineannotation>Result: </lineannotation><computeroutput>982384720.120000</computeroutput>
9551
-
9552
9554
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40.12');
9553
9555
<lineannotation>Result: </lineannotation><computeroutput>982355920.120000</computeroutput>
9554
-
9555
9556
SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
9556
9557
<lineannotation>Result: </lineannotation><computeroutput>442800.000000</computeroutput>
9557
9558
</screen>
@@ -9579,7 +9580,8 @@ SELECT to_timestamp(982384720.12);
9579
9580
<term><literal>hour</literal></term>
9580
9581
<listitem>
9581
9582
<para>
9582
- The hour field (0–23)
9583
+ The hour field (0–23 in timestamps, unrestricted in
9584
+ intervals)
9583
9585
</para>
9584
9586
9585
9587
<screen>
@@ -9614,7 +9616,7 @@ SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
9614
9616
<listitem>
9615
9617
<para>
9616
9618
The <acronym>ISO</acronym> 8601 week-numbering year that the date
9617
- falls in (not applicable to intervals)
9619
+ falls in
9618
9620
</para>
9619
9621
9620
9622
<screen>
@@ -9631,9 +9633,6 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
9631
9633
different from the Gregorian year. See the <literal>week</literal>
9632
9634
field for more information.
9633
9635
</para>
9634
- <para>
9635
- This field is not available in PostgreSQL releases prior to 8.3.
9636
- </para>
9637
9636
</listitem>
9638
9637
</varlistentry>
9639
9638
@@ -9642,7 +9641,7 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
9642
9641
<listitem>
9643
9642
<para>
9644
9643
The <firstterm>Julian Date</firstterm> corresponding to the
9645
- date or timestamp (not applicable to intervals) . Timestamps
9644
+ date or timestamp. Timestamps
9646
9645
that are not local midnight result in a fractional value. See
9647
9646
<xref linkend="datetime-julian-dates"/> for more information.
9648
9647
</para>
@@ -9675,12 +9674,15 @@ SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
9675
9674
<term><literal>millennium</literal></term>
9676
9675
<listitem>
9677
9676
<para>
9678
- The millennium
9677
+ The millennium; for <type>interval</type> values, the year field
9678
+ divided by 1000
9679
9679
</para>
9680
9680
9681
9681
<screen>
9682
9682
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
9683
9683
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
9684
+ SELECT EXTRACT(MILLENNIUM FROM INTERVAL '2001 years');
9685
+ <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
9684
9686
</screen>
9685
9687
9686
9688
<para>
@@ -9723,18 +9725,16 @@ SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
9723
9725
<term><literal>month</literal></term>
9724
9726
<listitem>
9725
9727
<para>
9726
- For <type>timestamp</type> values, thenumber of themonth
9727
- within the year (1–12) ; for <type>interval</type> values,
9728
- the number of months, modulo 12 (0–11)
9728
+ The number of themonth within theyear (1–12);
9729
+ for <type>interval</type> values, the number of months modulo 12
9730
+ (0–11)
9729
9731
</para>
9730
9732
9731
9733
<screen>
9732
9734
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
9733
9735
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
9734
-
9735
9736
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
9736
9737
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
9737
-
9738
9738
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
9739
9739
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
9740
9740
</screen>
@@ -9765,7 +9765,6 @@ SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
9765
9765
<screen>
9766
9766
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
9767
9767
<lineannotation>Result: </lineannotation><computeroutput>40.000000</computeroutput>
9768
-
9769
9768
SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
9770
9769
<lineannotation>Result: </lineannotation><computeroutput>28.500000</computeroutput>
9771
9770
</screen>
@@ -9847,6 +9846,20 @@ SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
9847
9846
</variablelist>
9848
9847
</para>
9849
9848
9849
+ <para>
9850
+ When processing an <type>interval</type> value,
9851
+ the <function>extract</function> function produces field values that
9852
+ match the interpretation used by the interval output function. This
9853
+ can produce surprising results if one starts with a non-normalized
9854
+ interval representation, for example:
9855
+ <screen>
9856
+ SELECT INTERVAL '80 minutes';
9857
+ <lineannotation>Result: </lineannotation><computeroutput>01:20:00</computeroutput>
9858
+ SELECT EXTRACT(MINUTES FROM INTERVAL '80 minutes');
9859
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9860
+ </screen>
9861
+ </para>
9862
+
9850
9863
<note>
9851
9864
<para>
9852
9865
When the input value is +/-Infinity, <function>extract</function> returns
@@ -9884,7 +9897,6 @@ date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
9884
9897
<screen>
9885
9898
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
9886
9899
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
9887
-
9888
9900
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
9889
9901
<lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
9890
9902
</screen>
@@ -9962,16 +9974,12 @@ date_trunc(<replaceable>field</replaceable>, <replaceable>source</replaceable> [
9962
9974
<screen>
9963
9975
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
9964
9976
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00</computeroutput>
9965
-
9966
9977
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
9967
9978
<lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00</computeroutput>
9968
-
9969
9979
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
9970
9980
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 00:00:00-05</computeroutput>
9971
-
9972
9981
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
9973
9982
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 08:00:00-05</computeroutput>
9974
-
9975
9983
SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
9976
9984
<lineannotation>Result: </lineannotation><computeroutput>3 days 02:00:00</computeroutput>
9977
9985
</screen>
@@ -10010,7 +10018,6 @@ date_bin(<replaceable>stride</replaceable>, <replaceable>source</replaceable>, <
10010
10018
<screen>
10011
10019
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01');
10012
10020
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:30:00</computeroutput>
10013
-
10014
10021
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30');
10015
10022
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:32:30</computeroutput>
10016
10023
</screen>
@@ -10134,10 +10141,8 @@ SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-0
10134
10141
<screen>
10135
10142
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'America/Denver';
10136
10143
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 19:38:40-08</computeroutput>
10137
-
10138
10144
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'America/Denver';
10139
10145
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 18:38:40</computeroutput>
10140
-
10141
10146
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'Asia/Tokyo' AT TIME ZONE 'America/Chicago';
10142
10147
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 05:38:40</computeroutput>
10143
10148
</screen>
@@ -10212,16 +10217,12 @@ LOCALTIMESTAMP(<replaceable>precision</replaceable>)
10212
10217
<screen>
10213
10218
SELECT CURRENT_TIME;
10214
10219
<lineannotation>Result: </lineannotation><computeroutput>14:39:53.662522-05</computeroutput>
10215
-
10216
10220
SELECT CURRENT_DATE;
10217
10221
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23</computeroutput>
10218
-
10219
10222
SELECT CURRENT_TIMESTAMP;
10220
10223
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522-05</computeroutput>
10221
-
10222
10224
SELECT CURRENT_TIMESTAMP(2);
10223
10225
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.66-05</computeroutput>
10224
-
10225
10226
SELECT LOCALTIMESTAMP;
10226
10227
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522</computeroutput>
10227
10228
</screen>