Home Explore Blog CI



postgresql

9th chunk of `doc/src/sgml/syntax.sgml`
3dcd944e85871204309959a5b9ca71d385c3d7109a38cd3d0000000100000fa2

     convenient way to write complicated string literals than the
     standard-compliant single quote syntax.  It is particularly useful when
     representing string constants inside other constants, as is often needed
     in procedural function definitions.  With single-quote syntax, each
     backslash in the above example would have to be written as four
     backslashes, which would be reduced to two backslashes in parsing the
     original string constant, and then to one when the inner string constant
     is re-parsed during function execution.
    </para>
   </sect3>

   <sect3 id="sql-syntax-bit-strings">
    <title>Bit-String Constants</title>

    <indexterm zone="sql-syntax-bit-strings">
     <primary>bit string</primary>
     <secondary>constant</secondary>
    </indexterm>

    <para>
     Bit-string constants look like regular string constants with a
     <literal>B</literal> (upper or lower case) immediately before the
     opening quote (no intervening whitespace), e.g.,
     <literal>B'1001'</literal>.  The only characters allowed within
     bit-string constants are <literal>0</literal> and
     <literal>1</literal>.
    </para>

    <para>
     Alternatively, bit-string constants can be specified in hexadecimal
     notation, using a leading <literal>X</literal> (upper or lower case),
     e.g., <literal>X'1FF'</literal>.  This notation is equivalent to
     a bit-string constant with four binary digits for each hexadecimal digit.
    </para>

    <para>
     Both forms of bit-string constant can be continued
     across lines in the same way as regular string constants.
     Dollar quoting cannot be used in a bit-string constant.
    </para>
   </sect3>

   <sect3 id="sql-syntax-constants-numeric">
    <title>Numeric Constants</title>

    <indexterm>
     <primary>number</primary>
     <secondary>constant</secondary>
    </indexterm>

    <para>
     Numeric constants are accepted in these general forms:
<synopsis>
<replaceable>digits</replaceable>
<replaceable>digits</replaceable>.<optional><replaceable>digits</replaceable></optional><optional>e<optional>+-</optional><replaceable>digits</replaceable></optional>
<optional><replaceable>digits</replaceable></optional>.<replaceable>digits</replaceable><optional>e<optional>+-</optional><replaceable>digits</replaceable></optional>
<replaceable>digits</replaceable>e<optional>+-</optional><replaceable>digits</replaceable>
</synopsis>
     where <replaceable>digits</replaceable> is one or more decimal
     digits (0 through 9).  At least one digit must be before or after the
     decimal point, if one is used.  At least one digit must follow the
     exponent marker (<literal>e</literal>), if one is present.
     There cannot be any spaces or other characters embedded in the
     constant, except for underscores, which can be used for visual grouping as
     described below.  Note that any leading plus or minus sign is not actually
     considered part of the constant; it is an 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

Title: Bit-String and Numeric Constants in PostgreSQL
Summary
This section explains bit-string constants, which resemble regular strings with a 'B' prefix (e.g., B'1001'), allowing only 0 and 1 characters. They can also be specified in hexadecimal notation with an 'X' prefix (e.g., X'1FF'). The section also describes numeric constants, which can be integers or floating-point numbers with optional exponents, and non-decimal integer constants like hexadecimal (0x...), octal (0o...), and binary (0b...) forms.