Home Explore Blog CI



postgresql

36th chunk of `doc/src/sgml/datatype.sgml`
008dc057dcbc5bbd63d3f4d821415414ee6a4f99e18ee50d0000000100000fa4
 '1'</literal> means 1 second.  Also, field values
     <quote>to the right</quote> of the least significant field allowed by the
     <replaceable>fields</replaceable> specification are silently discarded.  For
     example, writing <literal>INTERVAL '1 day 2:03:04' HOUR TO MINUTE</literal>
     results in dropping the seconds field, but not the day field.
    </para>

    <para>
     According to the <acronym>SQL</acronym> standard all fields of an interval
     value must have the same sign, so a leading negative sign applies to all
     fields; for example the negative sign in the interval literal
     <literal>'-1 2:03:04'</literal> applies to both the days and hour/minute/second
     parts.  <productname>PostgreSQL</productname> allows the fields to have different
     signs, and traditionally treats each field in the textual representation
     as independently signed, so that the hour/minute/second part is
     considered positive in this example.  If <varname>IntervalStyle</varname> is
     set to <literal>sql_standard</literal> then a leading sign is considered
     to apply to all fields (but only if no additional signs appear).
     Otherwise the traditional <productname>PostgreSQL</productname> interpretation is
     used.  To avoid ambiguity, it's recommended to attach an explicit sign
     to each field if any field is negative.
    </para>

    <para>
     Internally, <type>interval</type> values are stored as three integral
     fields: months, days, and microseconds.  These fields are kept
     separate because the number of days in a month varies, while a day
     can have 23 or 25 hours if a daylight savings time transition is
     involved.  An interval input string that uses other units is
     normalized into this format, and then reconstructed in a standardized
     way for output, for example:

<programlisting>
SELECT '2 years 15 months 100 weeks 99 hours 123456789 milliseconds'::interval;
               interval
---------------------------------------
 3 years 3 mons 700 days 133:17:36.789
</programlisting>

     Here weeks, which are understood as <quote>7 days</quote>, have been
     kept separate, while the smaller and larger time units were
     combined and normalized.
    </para>

    <para>
     Input field values can have fractional parts, for example <literal>'1.5
     weeks'</literal> or <literal>'01:02:03.45'</literal>.  However,
     because <type>interval</type> internally stores only integral fields,
     fractional values must be converted into smaller
     units.  Fractional parts of units greater than months are rounded to
     be an integer number of months, e.g. <literal>'1.5 years'</literal>
     becomes <literal>'1 year 6 mons'</literal>.  Fractional parts of
     weeks and days are computed to be an integer number of days and
     microseconds, assuming 30 days per month and 24 hours per day, e.g.,
     <literal>'1.75 months'</literal> becomes <literal>1 mon 22 days
     12:00:00</literal>.  Only seconds will ever be shown as fractional
     on output.
    </para>

    <para>
     <xref linkend="datatype-interval-input-examples"/> shows some examples
     of valid <type>interval</type> input.
    </para>

     <table id="datatype-interval-input-examples">
      <title>Interval Input</title>
      <tgroup cols="2">
       <thead>
        <row>
         <entry>Example</entry>
         <entry>Description</entry>
        </row>
       </thead>
       <tbody>
        <row>
         <entry><literal>1-2</literal></entry>
         <entry>SQL standard format: 1 year 2 months</entry>
        </row>
        <row>
         <entry><literal>3 4:05:06</literal></entry>
         <entry>SQL standard format: 3 days 4 hours 5 minutes 6 seconds</entry>
        </row>
        <row>
         <entry><literal>1 year 2 months 3 days 4 hours 5 minutes 6 seconds</literal></entry>
         <entry>Traditional Postgres format: 1 year 2 months 3 days 4 hours 5 minutes 6 seconds</entry>
        </row>
        <row>

Title: Interval Input and Output in PostgreSQL
Summary
PostgreSQL's interval data type stores values as months, days, and microseconds, and input strings are normalized into this format. Input field values can have fractional parts, which are converted into smaller units. The SQL standard requires all fields to have the same sign, but PostgreSQL allows different signs and treats each field as independently signed.Interval input strings can be in various formats, including SQL standard and traditional Postgres formats, and examples of valid input are provided.