@@ -8471,7 +8471,8 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
8471
8471
</para>
8472
8472
<para>
8473
8473
Subtract timestamps (converting 24-hour intervals into days,
8474
- similarly to <function>justify_hours()</function>)
8474
+ similarly to <link
8475
+ linkend="function-justify-hours"><function>justify_hours()</function></link>)
8475
8476
</para>
8476
8477
<para>
8477
8478
<literal>timestamp '2001-09-29 03:00' - timestamp '2001-07-27 12:00'</literal>
@@ -8826,35 +8827,35 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
8826
8827
8827
8828
<row>
8828
8829
<entry role="func_table_entry"><para role="func_signature">
8829
- <indexterm>
8830
+ <indexterm id="function-justify-days" >
8830
8831
<primary>justify_days</primary>
8831
8832
</indexterm>
8832
8833
<function>justify_days</function> ( <type>interval</type> )
8833
8834
<returnvalue>interval</returnvalue>
8834
8835
</para>
8835
8836
<para>
8836
- Adjust interval so 30-day time periodsare represented as months
8837
+ Adjust interval, converting 30-day time periodsto months
8837
8838
</para>
8838
8839
<para>
8839
- <literal>justify_days(interval '35 days')</literal>
8840
- <returnvalue>1mon 5 days</returnvalue>
8840
+ <literal>justify_days(interval '1 year 65 days')</literal>
8841
+ <returnvalue>1year 2 mons 5 days</returnvalue>
8841
8842
</para></entry>
8842
8843
</row>
8843
8844
8844
8845
<row>
8845
8846
<entry role="func_table_entry"><para role="func_signature">
8846
- <indexterm>
8847
+ <indexterm id="function-justify-hours" >
8847
8848
<primary>justify_hours</primary>
8848
8849
</indexterm>
8849
8850
<function>justify_hours</function> ( <type>interval</type> )
8850
8851
<returnvalue>interval</returnvalue>
8851
8852
</para>
8852
8853
<para>
8853
- Adjust interval so 24-hour time periodsare represented as days
8854
+ Adjust interval, converting 24-hour time periodsto days
8854
8855
</para>
8855
8856
<para>
8856
- <literal>justify_hours(interval '27 hours')</literal>
8857
- <returnvalue>1 day 03:00 :00</returnvalue>
8857
+ <literal>justify_hours(interval '50 hours 10 minutes ')</literal>
8858
+ <returnvalue>2 days 02:10 :00</returnvalue>
8858
8859
</para></entry>
8859
8860
</row>
8860
8861
@@ -9269,13 +9270,19 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9269
9270
The <function>extract</function> function retrieves subfields
9270
9271
such as year or hour from date/time values.
9271
9272
<replaceable>source</replaceable> must be a value expression of
9272
- type <type>timestamp</type>, <type>time </type>,or <type>interval </type>.
9273
- (Expressions of type <type>date </type> are
9274
- cast to <type>timestamp</type> and can therefore be used as
9275
- well.) <replaceable>field</replaceable> is an identifier or
9273
+ type <type>timestamp</type>, <type>date </type>, <type>time </type>,
9274
+ or <type>interval </type>. (Timestamps and times can be with or
9275
+ without time zone.)
9276
+ <replaceable>field</replaceable> is an identifier or
9276
9277
string that selects what field to extract from the source value.
9278
+ Not all fields are valid for every input data type; for example, fields
9279
+ smaller than a day cannot be extracted from a <type>date</type>, while
9280
+ fields of a day or more cannot be extracted from a <type>time</type>.
9277
9281
The <function>extract</function> function returns values of type
9278
9282
<type>double precision</type>.
9283
+ </para>
9284
+
9285
+ <para>
9279
9286
The following are valid field names:
9280
9287
9281
9288
<!-- alphabetical -->
@@ -9284,40 +9291,36 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
9284
9291
<term><literal>century</literal></term>
9285
9292
<listitem>
9286
9293
<para>
9287
- The century
9294
+ The century; for <type>interval</type> values, the year field
9295
+ divided by 100
9288
9296
</para>
9289
9297
9290
9298
<screen>
9291
9299
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
9292
9300
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9293
9301
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
9294
9302
<lineannotation>Result: </lineannotation><computeroutput>21</computeroutput>
9303
+ SELECT EXTRACT(CENTURY FROM DATE '0001-01-01 AD');
9304
+ <lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
9305
+ SELECT EXTRACT(CENTURY FROM DATE '0001-12-31 BC');
9306
+ <lineannotation>Result: </lineannotation><computeroutput>-1</computeroutput>
9307
+ SELECT EXTRACT(CENTURY FROM INTERVAL '2001 years');
9308
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9295
9309
</screen>
9296
-
9297
- <para>
9298
- The first century starts at 0001-01-01 00:00:00 AD, although
9299
- they did not know it at the time. This definition applies to all
9300
- Gregorian calendar countries. There is no century number 0,
9301
- you go from -1 century to 1 century.
9302
-
9303
- If you disagree with this, please write your complaint to:
9304
- Pope, Cathedral Saint-Peter of Roma, Vatican.
9305
- </para>
9306
9310
</listitem>
9307
9311
</varlistentry>
9308
9312
9309
9313
<varlistentry>
9310
9314
<term><literal>day</literal></term>
9311
9315
<listitem>
9312
9316
<para>
9313
- For <type>timestamp</type> values, the day( of the month) field
9314
- (1–31) ; for <type>interval</type> values, the number of days
9317
+ The day of the month (1–31); for <type>interval</type>
9318
+ values, the number of days
9315
9319
</para>
9316
9320
9317
9321
<screen>
9318
9322
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
9319
9323
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
9320
-
9321
9324
SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
9322
9325
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
9323
9326
</screen>
@@ -9391,10 +9394,8 @@ SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
9391
9394
<screen>
9392
9395
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
9393
9396
<lineannotation>Result: </lineannotation><computeroutput>982384720.12</computeroutput>
9394
-
9395
9397
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40.12');
9396
9398
<lineannotation>Result: </lineannotation><computeroutput>982355920.12</computeroutput>
9397
-
9398
9399
SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
9399
9400
<lineannotation>Result: </lineannotation><computeroutput>442800</computeroutput>
9400
9401
</screen>
@@ -9422,7 +9423,8 @@ SELECT to_timestamp(982384720.12);
9422
9423
<term><literal>hour</literal></term>
9423
9424
<listitem>
9424
9425
<para>
9425
- The hour field (0–23)
9426
+ The hour field (0–23 in timestamps, unrestricted in
9427
+ intervals)
9426
9428
</para>
9427
9429
9428
9430
<screen>
@@ -9457,7 +9459,7 @@ SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
9457
9459
<listitem>
9458
9460
<para>
9459
9461
The <acronym>ISO</acronym> 8601 week-numbering year that the date
9460
- falls in (not applicable to intervals)
9462
+ falls in
9461
9463
</para>
9462
9464
9463
9465
<screen>
@@ -9474,9 +9476,6 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
9474
9476
different from the Gregorian year. See the <literal>week</literal>
9475
9477
field for more information.
9476
9478
</para>
9477
- <para>
9478
- This field is not available in PostgreSQL releases prior to 8.3.
9479
- </para>
9480
9479
</listitem>
9481
9480
</varlistentry>
9482
9481
@@ -9485,7 +9484,7 @@ SELECT EXTRACT(ISOYEAR FROM DATE '2006-01-02');
9485
9484
<listitem>
9486
9485
<para>
9487
9486
The <firstterm>Julian Date</firstterm> corresponding to the
9488
- date or timestamp (not applicable to intervals) . Timestamps
9487
+ date or timestamp. Timestamps
9489
9488
that are not local midnight result in a fractional value. See
9490
9489
<xref linkend="datetime-julian-dates"/> for more information.
9491
9490
</para>
@@ -9518,12 +9517,15 @@ SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
9518
9517
<term><literal>millennium</literal></term>
9519
9518
<listitem>
9520
9519
<para>
9521
- The millennium
9520
+ The millennium; for <type>interval</type> values, the year field
9521
+ divided by 1000
9522
9522
</para>
9523
9523
9524
9524
<screen>
9525
9525
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
9526
9526
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
9527
+ SELECT EXTRACT(MILLENNIUM FROM INTERVAL '2001 years');
9528
+ <lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
9527
9529
</screen>
9528
9530
9529
9531
<para>
@@ -9566,18 +9568,16 @@ SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
9566
9568
<term><literal>month</literal></term>
9567
9569
<listitem>
9568
9570
<para>
9569
- For <type>timestamp</type> values, thenumber of themonth
9570
- within the year (1–12) ; for <type>interval</type> values,
9571
- the number of months, modulo 12 (0–11)
9571
+ The number of themonth within theyear (1–12);
9572
+ for <type>interval</type> values, the number of months modulo 12
9573
+ (0–11)
9572
9574
</para>
9573
9575
9574
9576
<screen>
9575
9577
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
9576
9578
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
9577
-
9578
9579
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
9579
9580
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
9580
-
9581
9581
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
9582
9582
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
9583
9583
</screen>
@@ -9608,7 +9608,6 @@ SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
9608
9608
<screen>
9609
9609
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
9610
9610
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
9611
-
9612
9611
SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
9613
9612
<lineannotation>Result: </lineannotation><computeroutput>28.5</computeroutput>
9614
9613
</screen>
@@ -9690,6 +9689,20 @@ SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
9690
9689
</variablelist>
9691
9690
</para>
9692
9691
9692
+ <para>
9693
+ When processing an <type>interval</type> value,
9694
+ the <function>extract</function> function produces field values that
9695
+ match the interpretation used by the interval output function. This
9696
+ can produce surprising results if one starts with a non-normalized
9697
+ interval representation, for example:
9698
+ <screen>
9699
+ SELECT INTERVAL '80 minutes';
9700
+ <lineannotation>Result: </lineannotation><computeroutput>01:20:00</computeroutput>
9701
+ SELECT EXTRACT(MINUTES FROM INTERVAL '80 minutes');
9702
+ <lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
9703
+ </screen>
9704
+ </para>
9705
+
9693
9706
<note>
9694
9707
<para>
9695
9708
When the input value is +/-Infinity, <function>extract</function> returns
@@ -9723,7 +9736,6 @@ date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
9723
9736
<screen>
9724
9737
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
9725
9738
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
9726
-
9727
9739
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
9728
9740
<lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
9729
9741
</screen>
@@ -9801,16 +9813,12 @@ date_trunc(<replaceable>field</replaceable>, <replaceable>source</replaceable> [
9801
9813
<screen>
9802
9814
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
9803
9815
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00</computeroutput>
9804
-
9805
9816
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
9806
9817
<lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00</computeroutput>
9807
-
9808
9818
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
9809
9819
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 00:00:00-05</computeroutput>
9810
-
9811
9820
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
9812
9821
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 08:00:00-05</computeroutput>
9813
-
9814
9822
SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
9815
9823
<lineannotation>Result: </lineannotation><computeroutput>3 days 02:00:00</computeroutput>
9816
9824
</screen>
@@ -9923,10 +9931,8 @@ SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
9923
9931
<screen>
9924
9932
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'America/Denver';
9925
9933
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 19:38:40-08</computeroutput>
9926
-
9927
9934
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'America/Denver';
9928
9935
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 18:38:40</computeroutput>
9929
-
9930
9936
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'Asia/Tokyo' AT TIME ZONE 'America/Chicago';
9931
9937
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 05:38:40</computeroutput>
9932
9938
</screen>
@@ -10001,16 +10007,12 @@ LOCALTIMESTAMP(<replaceable>precision</replaceable>)
10001
10007
<screen>
10002
10008
SELECT CURRENT_TIME;
10003
10009
<lineannotation>Result: </lineannotation><computeroutput>14:39:53.662522-05</computeroutput>
10004
-
10005
10010
SELECT CURRENT_DATE;
10006
10011
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23</computeroutput>
10007
-
10008
10012
SELECT CURRENT_TIMESTAMP;
10009
10013
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522-05</computeroutput>
10010
-
10011
10014
SELECT CURRENT_TIMESTAMP(2);
10012
10015
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.66-05</computeroutput>
10013
-
10014
10016
SELECT LOCALTIMESTAMP;
10015
10017
<lineannotation>Result: </lineannotation><computeroutput>2019-12-23 14:39:53.662522</computeroutput>
10016
10018
</screen>