Home Explore Blog CI



postgresql

37th chunk of `doc/src/sgml/datatype.sgml`
bcf5b4d4e0ef1acd86847174b32493f5690f7b1c5160bfeb0000000100000fa8
 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>
         <entry><literal>P1Y2M3DT4H5M6S</literal></entry>
         <entry>ISO 8601 <quote>format with designators</quote>: same meaning as above</entry>
        </row>
        <row>
         <entry><literal>P0001-02-03T04:05:06</literal></entry>
         <entry>ISO 8601 <quote>alternative format</quote>: same meaning as above</entry>
        </row>
       </tbody>
      </tgroup>
     </table>

   </sect2>

   <sect2 id="datatype-interval-output">
    <title>Interval Output</title>

    <indexterm>
     <primary>interval</primary>
     <secondary>output format</secondary>
     <seealso>formatting</seealso>
    </indexterm>

    <para>
     As previously explained, <productname>PostgreSQL</productname>
     stores <type>interval</type> values as months, days, and
     microseconds.  For output, the months field is converted to years and
     months by dividing by 12.  The days field is shown as-is.  The
     microseconds field is converted to hours, minutes, seconds, and
     fractional seconds.  Thus months, minutes, and seconds will never be
     shown as exceeding the ranges 0&ndash;11, 0&ndash;59, and 0&ndash;59
     respectively, while the displayed years, days, and hours fields can
     be quite large.  (The <link
     linkend="function-justify-days"><function>justify_days</function></link>
     and <link
     linkend="function-justify-hours"><function>justify_hours</function></link>
     functions can be used if it is desirable to transpose large days or
     hours values into the next higher field.)
    </para>

    <para>
     The output format of the interval type can be set to one of the
     four styles <literal>sql_standard</literal>, <literal>postgres</literal>,
     <literal>postgres_verbose</literal>, or <literal>iso_8601</literal>,
     using the command <literal>SET intervalstyle</literal>.
     The default is the <literal>postgres</literal> format.
     <xref linkend="interval-style-output-table"/> shows examples of each
     output style.
    </para>

    <para>
     The <literal>sql_standard</literal> style produces output that conforms to
     the SQL standard's specification for interval literal strings, if
     the interval value meets the standard's restrictions (either year-month
     only or day-time only, with no mixing of positive
     and negative components).  Otherwise the output looks like a standard
     year-month literal string followed by a day-time literal string,
     with explicit signs added to disambiguate mixed-sign intervals.
    </para>

    <para>
     The output of the <literal>postgres</literal> style matches the output of
     <productname>PostgreSQL</productname> releases prior to 8.4 when the
     <xref linkend="guc-datestyle"/> parameter was set to <literal>ISO</literal>.
    </para>

    <para>
     The output of the <literal>postgres_verbose</literal> style matches the output of
     <productname>PostgreSQL</productname> releases prior to 8.4 when the
     <varname>DateStyle</varname> parameter

Title: Interval Output in PostgreSQL
Summary
PostgreSQL stores interval values as months, days, and microseconds, and converts them to years, months, days, hours, minutes, seconds, and fractional seconds for output. The output format can be set to one of four styles: sql_standard, postgres, postgres_verbose, or iso_8601, using the SET intervalstyle command. Each style produces output in a specific format, with sql_standard conforming to the SQL standard and postgres matching the output of earlier PostgreSQL releases.