Home Explore Blog CI



postgresql

10th chunk of `doc/src/sgml/syntax.sgml`
dade5a257bdbd19828cb9286242a3ee9a659f76d0f3db8c00000000100000fa2
 operator applied to the
     constant.
    </para>

    <para>
     These are some examples of valid numeric constants:
<literallayout>
42
3.5
4.
.001
5e2
1.925e-3
</literallayout>
    </para>

    <para>
     Additionally, non-decimal integer constants are accepted in these forms:
<synopsis>
0x<replaceable>hexdigits</replaceable>
0o<replaceable>octdigits</replaceable>
0b<replaceable>bindigits</replaceable>
</synopsis>
     where <replaceable>hexdigits</replaceable> is one or more hexadecimal digits
     (0-9, A-F), <replaceable>octdigits</replaceable> is one or more octal
     digits (0-7), and <replaceable>bindigits</replaceable> is one or more binary
     digits (0 or 1).  Hexadecimal digits and the radix prefixes can be in
     upper or lower case.  Note that only integers can have non-decimal forms,
     not numbers with fractional parts.
    </para>

    <para>
     These are some examples of valid non-decimal integer constants:
<literallayout>
0b100101
0B10011001
0o273
0O755
0x42f
0XFFFF
</literallayout>
    </para>

    <para>
     For visual grouping, underscores can be inserted between digits.  These
     have no further effect on the value of the constant.  For example:
<literallayout>
1_500_000_000
0b10001000_00000000
0o_1_755
0xFFFF_FFFF
1.618_034
</literallayout>
     Underscores are not allowed at the start or end of a numeric constant or
     a group of digits (that is, immediately before or after the decimal point
     or the exponent marker), and more than one underscore in a row is not
     allowed.
    </para>

    <para>
     <indexterm><primary>integer</primary></indexterm>
     <indexterm><primary>bigint</primary></indexterm>
     <indexterm><primary>numeric</primary></indexterm>
     A numeric constant that contains neither a decimal point nor an
     exponent is initially presumed to be type <type>integer</type> if its
     value fits in type <type>integer</type> (32 bits); otherwise it is
     presumed to be type <type>bigint</type> if its
     value fits in type <type>bigint</type> (64 bits); otherwise it is
     taken to be type <type>numeric</type>.  Constants that contain decimal
     points and/or exponents are always initially presumed to be type
     <type>numeric</type>.
    </para>

    <para>
     The initially assigned data type of a numeric constant is just a
     starting point for the type resolution algorithms.  In most cases
     the constant will be automatically coerced to the most
     appropriate type depending on context.  When necessary, you can
     force a numeric value to be interpreted as a specific data type
     by casting it.<indexterm><primary>type cast</primary></indexterm>
     For example, you can force a numeric value to be treated as type
     <type>real</type> (<type>float4</type>) by writing:

<programlisting>
REAL '1.23'  -- string style
1.23::REAL   -- PostgreSQL (historical) style
</programlisting>

     These are actually just special cases of the general casting
     notations discussed next.
    </para>
   </sect3>

   <sect3 id="sql-syntax-constants-generic">
    <title>Constants of Other Types</title>

    <indexterm>
     <primary>data type</primary>
     <secondary>constant</secondary>
    </indexterm>

    <para>
     A constant of an <emphasis>arbitrary</emphasis> type can be
     entered using any one of the following notations:
<synopsis>
<replaceable>type</replaceable> '<replaceable>string</replaceable>'
'<replaceable>string</replaceable>'::<replaceable>type</replaceable>
CAST ( '<replaceable>string</replaceable>' AS <replaceable>type</replaceable> )
</synopsis>
     The string constant's text is passed to the input conversion
     routine for the type called <replaceable>type</replaceable>. The
     result is a constant of the indicated type.  The explicit type
     cast can be omitted if there is no ambiguity as to the type the
     constant must be (for example, when it is assigned directly to a
     table column), in which case it is automatically

Title: Numeric Constants: Forms, Underscores, and Data Types
Summary
This section details the format of numeric constants in PostgreSQL, including decimal and non-decimal (hexadecimal, octal, and binary) integer forms. It explains the use of underscores for visual grouping within numbers. It also describes how the system initially infers the data type of a numeric constant (integer, bigint, or numeric) based on its value and format, and how explicit type casting can be used to force a specific data type.