@@ -9018,7 +9018,8 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
9018
9018
</para>
9019
9019
<para>
9020
9020
Subtract timestamps (converting 24-hour intervals into days,
9021
- similarly to <function>justify_hours()</function>)
9021
+ similarly to <link
9022
+ linkend="function-justify-hours"><function>justify_hours()</function></link>)
9022
9023
</para>
9023
9024
<para>
9024
9025
<literal>timestamp '2001-09-29 03:00' - timestamp '2001-07-27 12:00'</literal>
@@ -9387,35 +9388,35 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
9387
9388
9388
9389
<row>
9389
9390
<entry role="func_table_entry"><para role="func_signature">
9390
- <indexterm>
9391
+ <indexterm id="function-justify-days" >
9391
9392
<primary>justify_days</primary>
9392
9393
</indexterm>
9393
9394
<function>justify_days</function> ( <type>interval</type> )
9394
9395
<returnvalue>interval</returnvalue>
9395
9396
</para>
9396
9397
<para>
9397
- Adjust interval so 30-day time periodsare represented as months
9398
+ Adjust interval, converting 30-day time periodsto months
9398
9399
</para>
9399
9400
<para>
9400
- <literal>justify_days(interval '35 days')</literal>
9401
- <returnvalue>1mon 5 days</returnvalue>
9401
+ <literal>justify_days(interval '1 year 65 days')</literal>
9402
+ <returnvalue>1year 2 mons 5 days</returnvalue>
9402
9403
</para></entry>
9403
9404
</row>
9404
9405
9405
9406
<row>
9406
9407
<entry role="func_table_entry"><para role="func_signature">
9407
- <indexterm>
9408
+ <indexterm id="function-justify-hours" >
9408
9409
<primary>justify_hours</primary>
9409
9410
</indexterm>
9410
9411
<function>justify_hours</function> ( <type>interval</type> )
9411
9412
<returnvalue>interval</returnvalue>
9412
9413
</para>
9413
9414
<para>
9414
- Adjust interval so 24-hour time periodsare represented as days
9415
+ Adjust interval, converting 24-hour time periodsto days
9415
9416
</para>
9416
9417
<para>
9417
- <literal>justify_hours(interval '27 hours')</literal>
9418
- <returnvalue>1 day 03:00 :00</returnvalue>
9418
+ <literal>justify_hours(interval '50 hours 10 minutes ')</literal>
9419
+ <returnvalue>2 days 02:10 :00</returnvalue>
9419
9420
</para></entry>
9420
9421
</row>
9421
9422
@@ -9838,13 +9839,19 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9838
9839
The <function>extract</function> function retrieves subfields
9839
9840
such as year or hour from date/time values.
9840
9841
<replaceable>source</replaceable> must be a value expression of
9841
- type <type>timestamp</type>, <type>time </type>,or <type>interval </type>.
9842
- (Expressions of type <type>date </type> are
9843
- cast to <type>timestamp</type> and can therefore be used as
9844
- well.) <replaceable>field</replaceable> is an identifier or
9842
+ type <type>timestamp</type>, <type>date </type>, <type>time </type>,
9843
+ or <type>interval </type>. (Timestamps and times can be with or
9844
+ without time zone.)
9845
+ <replaceable>field</replaceable> is an identifier or
9845
9846
string that selects what field to extract from the source value.
9847
+ Not all fields are valid for every input data type; for example, fields
9848
+ smaller than a day cannot be extracted from a <type>date</type>, while
9849
+ fields of a day or more cannot be extracted from a <type>time</type>.
9846
9850
The <function>extract</function> function returns values of type
9847
9851
<type>numeric</type>.
9852
+ </para>
9853
+
9854
+ <para>
9848
9855
The following are valid field names:
9849
9856
9850
9857
<!-- alphabetical -->
@@ -9853,40 +9860,36 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9853
9860
<term><literal>century</literal></term>
9854
9861
<listitem>
9855
9862
<para>
9856
- The century
9863
+ The century; for <type>interval</type> values, the year field
9864
+ divided by 100
9857
9865
</para>
9858
9866
9859
9867
<screen>
9860
9868
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
9861
9869
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9862
9870
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
9863
9871
<lineannotation>Result: </lineannotation><computeroutput>21</computeroutput>
9872
+ SELECT EXTRACT(CENTURY FROM DATE '0001-01-01 AD');
9873
+ <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
9874
+ SELECT EXTRACT(CENTURY FROM DATE '0001-12-31 BC');
9875
+ <lineannotation>Result: </lineannotation><computeroutput>-1</computeroutput>
9876
+ SELECT EXTRACT(CENTURY FROM INTERVAL '2001 years');
9877
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9864
9878
</screen>
9865
-
9866
- <para>
9867
- The first century starts at 0001-01-01 00:00:00 AD, although
9868
- they did not know it at the time. This definition applies to all
9869
- Gregorian calendar countries. There is no century number 0,
9870
- you go from -1 century to 1 century.
9871
-
9872
- If you disagree with this, please write your complaint to:
9873
- Pope, Cathedral Saint-Peter of Roma, Vatican.
9874
- </para>
9875
9879
</listitem>
9876
9880
</varlistentry>
9877
9881
9878
9882
<varlistentry>
9879
9883
<term><literal>day</literal></term>
9880
9884
<listitem>
9881
9885
<para>
9882
- For <type>timestamp</type> values, the day( of the month) field
9883
- (1–31) ; for <type>interval</type> values, the number of days
9886
+ The day of the month (1–31); for <type>interval</type>
9887
+ values, the number of days
9884
9888
</para>
9885
9889
9886
9890
<screen>
9887
9891
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
9888
9892
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
9889
-
9890
9893
SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
9891
9894
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
9892
9895
</screen>
@@ -9960,10 +9963,8 @@ SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
9960
9963
<screen>
9961
9964
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
9962
9965
<lineannotation>Result: </lineannotation><computeroutput>982384720.120000</computeroutput>
9963
-
9964
9966
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40.12');
9965
9967
<lineannotation>Result: </lineannotation><computeroutput>982355920.120000</computeroutput>
9966
-
9967
9968
SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
9968
9969
<lineannotation>Result: </lineannotation><computeroutput>442800.000000</computeroutput>
9969
9970
</screen>
@@ -9991,7 +9992,8 @@ SELECT to_timestamp(982384720.12);
9991
9992
<term><literal>hour</literal></term>
9992
9993
<listitem>
9993
9994
<para>
9994
- The hour field (0–23)
9995
+ The hour field (0–23 in timestamps, unrestricted in
9996
+ intervals)
9995
9997
</para>
9996
9998
9997
9999
<screen>
@@ -10026,7 +10028,7 @@ SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
10026
10028
<listitem>
10027
10029
<para>
10028
10030
The <acronym>ISO</acronym> 8601 week-numbering year that the date
10029
- falls in (not applicable to intervals)
10031
+ falls in
10030
10032
</para>
10031
10033
10032
10034
<screen>
@@ -10043,9 +10045,6 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
10043
10045
different from the Gregorian year. See the <literal>week</literal>
10044
10046
field for more information.
10045
10047
</para>
10046
- <para>
10047
- This field is not available in PostgreSQL releases prior to 8.3.
10048
- </para>
10049
10048
</listitem>
10050
10049
</varlistentry>
10051
10050
@@ -10054,7 +10053,7 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
10054
10053
<listitem>
10055
10054
<para>
10056
10055
The <firstterm>Julian Date</firstterm> corresponding to the
10057
- date or timestamp (not applicable to intervals) . Timestamps
10056
+ date or timestamp. Timestamps
10058
10057
that are not local midnight result in a fractional value. See
10059
10058
<xref linkend="datetime-julian-dates"/> for more information.
10060
10059
</para>
@@ -10087,12 +10086,15 @@ SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
10087
10086
<term><literal>millennium</literal></term>
10088
10087
<listitem>
10089
10088
<para>
10090
- The millennium
10089
+ The millennium; for <type>interval</type> values, the year field
10090
+ divided by 1000
10091
10091
</para>
10092
10092
10093
10093
<screen>
10094
10094
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
10095
10095
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
10096
+ SELECT EXTRACT(MILLENNIUM FROM INTERVAL '2001 years');
10097
+ <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
10096
10098
</screen>
10097
10099
10098
10100
<para>
@@ -10135,18 +10137,16 @@ SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
10135
10137
<term><literal>month</literal></term>
10136
10138
<listitem>
10137
10139
<para>
10138
- For <type>timestamp</type> values, thenumber of themonth
10139
- within the year (1–12) ; for <type>interval</type> values,
10140
- the number of months, modulo 12 (0–11)
10140
+ The number of themonth within theyear (1–12);
10141
+ for <type>interval</type> values, the number of months modulo 12
10142
+ (0–11)
10141
10143
</para>
10142
10144
10143
10145
<screen>
10144
10146
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
10145
10147
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
10146
-
10147
10148
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
10148
10149
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
10149
-
10150
10150
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
10151
10151
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
10152
10152
</screen>
@@ -10177,7 +10177,6 @@ SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
10177
10177
<screen>
10178
10178
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
10179
10179
<lineannotation>Result: </lineannotation><computeroutput>40.000000</computeroutput>
10180
-
10181
10180
SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
10182
10181
<lineannotation>Result: </lineannotation><computeroutput>28.500000</computeroutput>
10183
10182
</screen>
@@ -10259,6 +10258,20 @@ SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
10259
10258
</variablelist>
10260
10259
</para>
10261
10260
10261
+ <para>
10262
+ When processing an <type>interval</type> value,
10263
+ the <function>extract</function> function produces field values that
10264
+ match the interpretation used by the interval output function. This
10265
+ can produce surprising results if one starts with a non-normalized
10266
+ interval representation, for example:
10267
+ <screen>
10268
+ SELECT INTERVAL '80 minutes';
10269
+ <lineannotation>Result: </lineannotation><computeroutput>01:20:00</computeroutput>
10270
+ SELECT EXTRACT(MINUTES FROM INTERVAL '80 minutes');
10271
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
10272
+ </screen>
10273
+ </para>
10274
+
10262
10275
<note>
10263
10276
<para>
10264
10277
When the input value is +/-Infinity, <function>extract</function> returns
@@ -10296,7 +10309,6 @@ date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
10296
10309
<screen>
10297
10310
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
10298
10311
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
10299
-
10300
10312
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
10301
10313
<lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
10302
10314
</screen>
@@ -10374,16 +10386,12 @@ date_trunc(<replaceable>field</replaceable>, <replaceable>source</replaceable> [
10374
10386
<screen>
10375
10387
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
10376
10388
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00</computeroutput>
10377
-
10378
10389
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
10379
10390
<lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00</computeroutput>
10380
-
10381
10391
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
10382
10392
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 00:00:00-05</computeroutput>
10383
-
10384
10393
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
10385
10394
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 08:00:00-05</computeroutput>
10386
-
10387
10395
SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
10388
10396
<lineannotation>Result: </lineannotation><computeroutput>3 days 02:00:00</computeroutput>
10389
10397
</screen>
@@ -10422,7 +10430,6 @@ date_bin(<replaceable>stride</replaceable>, <replaceable>source</replaceable>, <
10422
10430
<screen>
10423
10431
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01');
10424
10432
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:30:00</computeroutput>
10425
-
10426
10433
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30');
10427
10434
<lineannotation>Result: </lineannotation><computeroutput>2020-02-11 15:32:30</computeroutput>
10428
10435
</screen>
@@ -10546,10 +10553,8 @@ SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-0
10546
10553
<screen>
10547
10554
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'America/Denver';
10548
10555
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 19:38:40-08</computeroutput>
10549
-
10550
10556
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'America/Denver';
10551
10557
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 18:38:40</computeroutput>
10552
-
10553
10558
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'Asia/Tokyo' AT TIME ZONE 'America/Chicago';
10554
10559
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 05:38:40</computeroutput>
10555
10560
</screen>
@@ -10624,16 +10629,12 @@ LOCALTIMESTAMP(<replaceable>precision</replaceable>)
10624
10629
<screen>
10625
10630
SELECT CURRENT_TIME;
10626
10631
<lineannotation>Result: </lineannotation><computeroutput>14:39:53.662522-05</computeroutput>
10627
-
10628
10632
SELECT CURRENT_DATE;
10629
10633
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23</computeroutput>
10630
-
10631
10634
SELECT CURRENT_TIMESTAMP;
10632
10635
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522-05</computeroutput>
10633
-
10634
10636
SELECT CURRENT_TIMESTAMP(2);
10635
10637
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.66-05</computeroutput>
10636
-
10637
10638
SELECT LOCALTIMESTAMP;
10638
10639
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522</computeroutput>
10639
10640
</screen>