Home Explore Blog CI



postgresql

99th chunk of `doc/src/sgml/func.sgml`
9cff000711b1657c4ea0fe50cc117eebaeeba55e9a25636d0000000100000fa3
 zone</type>, <type>text</type> )
        <returnvalue>text</returnvalue>
       </para>
       <para>
        Converts time stamp to string according to the given format.
       </para>
       <para>
        <literal>to_char(timestamp '2002-04-20 17:31:12.66', 'HH12:MI:SS')</literal>
        <returnvalue>05:31:12</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <function>to_char</function> ( <type>interval</type>, <type>text</type> )
        <returnvalue>text</returnvalue>
       </para>
       <para>
        Converts interval to string according to the given format.
       </para>
       <para>
       <literal>to_char(interval '15h 2m 12s', 'HH24:MI:SS')</literal>
       <returnvalue>15:02:12</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <function>to_char</function> ( <replaceable>numeric_type</replaceable>, <type>text</type> )
        <returnvalue>text</returnvalue>
       </para>
       <para>
        Converts number to string according to the given format; available
        for <type>integer</type>, <type>bigint</type>, <type>numeric</type>,
        <type>real</type>, <type>double precision</type>.
       </para>
       <para>
        <literal>to_char(125, '999')</literal>
        <returnvalue>125</returnvalue>
       </para>
       <para>
        <literal>to_char(125.8::real, '999D9')</literal>
        <returnvalue>125.8</returnvalue>
       </para>
       <para>
        <literal>to_char(-125.8, '999D99S')</literal>
        <returnvalue>125.80-</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm>
         <primary>to_date</primary>
        </indexterm>
        <function>to_date</function> ( <type>text</type>, <type>text</type> )
        <returnvalue>date</returnvalue>
       </para>
       <para>
        Converts string to date according to the given format.
       </para>
       <para>
        <literal>to_date('05 Dec 2000', 'DD Mon YYYY')</literal>
        <returnvalue>2000-12-05</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm>
         <primary>to_number</primary>
        </indexterm>
        <function>to_number</function> ( <type>text</type>, <type>text</type> )
        <returnvalue>numeric</returnvalue>
       </para>
       <para>
        Converts string to numeric according to the given format.
       </para>
       <para>
        <literal>to_number('12,454.8-', '99G999D9S')</literal>
        <returnvalue>-12454.8</returnvalue>
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm>
         <primary>to_timestamp</primary>
        </indexterm>
        <function>to_timestamp</function> ( <type>text</type>, <type>text</type> )
        <returnvalue>timestamp with time zone</returnvalue>
       </para>
       <para>
        Converts string to time stamp according to the given format.
        (See also <function>to_timestamp(double precision)</function> in
        <xref linkend="functions-datetime-table"/>.)
       </para>
       <para>
        <literal>to_timestamp('05 Dec 2000', 'DD Mon YYYY')</literal>
        <returnvalue>2000-12-05 00:00:00-05</returnvalue>
       </para></entry>
      </row>
     </tbody>
    </tgroup>
   </table>

   <tip>
    <para>
     <function>to_timestamp</function> and <function>to_date</function>
     exist to handle input formats that cannot be converted by
     simple casting.  For most standard date/time formats, simply casting the
     source string to the required data type works, and is much easier.
     Similarly, <function>to_number</function> is unnecessary for standard numeric
     representations.
    </para>
   </tip>

   <para>

Title: PostgreSQL Formatting Functions: to_char, to_date, to_number, and to_timestamp
Summary
This section details PostgreSQL's formatting functions, including `to_char` (for timestamp, interval, and numeric types), `to_date`, `to_number`, and `to_timestamp`. It provides function signatures, descriptions, and examples for each, demonstrating how to convert various data types to formatted strings and vice versa. It also notes that casting is easier for standard date/time formats and that `to_number` is unnecessary for standard numeric representations.