Home Explore Blog CI



postgresql

4th chunk of `doc/src/sgml/syntax.sgml`
7bc60fadfb0cc83ca644f92dd1486b13115bdf8f65da9bf80000000100000fa0
 the
    four-digit hexadecimal code point number or alternatively a
    backslash followed by a plus sign followed by a six-digit
    hexadecimal code point number.  For example, the
    identifier <literal>"data"</literal> could be written as
<programlisting>
U&amp;"d\0061t\+000061"
</programlisting>
    The following less trivial example writes the Russian
    word <quote>slon</quote> (elephant) in Cyrillic letters:
<programlisting>
U&amp;"\0441\043B\043E\043D"
</programlisting>
   </para>

   <para>
    If a different escape character than backslash is desired, it can
    be specified using
    the <literal>UESCAPE</literal><indexterm><primary>UESCAPE</primary></indexterm>
    clause after the string, for example:
<programlisting>
U&amp;"d!0061t!+000061" UESCAPE '!'
</programlisting>
    The escape character can be any single character other than a
    hexadecimal digit, the plus sign, a single quote, a double quote,
    or a whitespace character.  Note that the escape character is
    written in single quotes, not double quotes,
    after <literal>UESCAPE</literal>.
   </para>

   <para>
    To include the escape character in the identifier literally, write
    it twice.
   </para>

   <para>
    Either the 4-digit or the 6-digit escape form can be used to
    specify UTF-16 surrogate pairs to compose characters with code
    points larger than U+FFFF, although the availability of the
    6-digit form technically makes this unnecessary.  (Surrogate
    pairs are not stored directly, but are combined into a single
    code point.)
   </para>

   <para>
    If the server encoding is not UTF-8, the Unicode code point identified
    by one of these escape sequences is converted to the actual server
    encoding; an error is reported if that's not possible.
   </para>
  </sect2>


  <sect2 id="sql-syntax-constants">
   <title>Constants</title>

   <indexterm zone="sql-syntax-constants">
    <primary>constant</primary>
   </indexterm>

   <para>
    There are three kinds of <firstterm>implicitly-typed
    constants</firstterm> in <productname>PostgreSQL</productname>:
    strings, bit strings, and numbers.
    Constants can also be specified with explicit types, which can
    enable more accurate representation and more efficient handling by
    the system. These alternatives are discussed in the following
    subsections.
   </para>

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

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

    <para>
     <indexterm>
      <primary>quotation marks</primary>
      <secondary>escaping</secondary>
     </indexterm>
     A string constant in SQL is an arbitrary sequence of characters
     bounded by single quotes (<literal>'</literal>), for example
     <literal>'This is a string'</literal>.  To include
     a single-quote character within a string constant,
     write two adjacent single quotes, e.g.,
     <literal>'Dianne''s horse'</literal>.
     Note that this is <emphasis>not</emphasis> the same as a double-quote
     character (<literal>"</literal>). <!-- font-lock sanity: " -->
    </para>

    <para>
     Two string constants that are only separated by whitespace
     <emphasis>with at least one newline</emphasis> are concatenated
     and effectively treated as if the string had been written as one
     constant.  For example:
<programlisting>
SELECT 'foo'
'bar';
</programlisting>
     is equivalent to:
<programlisting>
SELECT 'foobar';
</programlisting>
     but:
<programlisting>
SELECT 'foo'      'bar';
</programlisting>
     is not valid syntax.  (This slightly bizarre behavior is specified
     by <acronym>SQL</acronym>; <productname>PostgreSQL</productname> is
     following the standard.)
    </para>
   </sect3>

   <sect3 id="sql-syntax-strings-escape">
    <title>String Constants with C-Style Escapes</title>

     <indexterm zone="sql-syntax-strings-escape">
     

Title: Unicode Escapes, String Constants, and C-Style Escapes
Summary
This section details the use of Unicode escapes in SQL identifiers, including how to specify different escape characters and handle UTF-16 surrogate pairs. It also covers string constants, including how to escape single quotes and concatenate string literals separated by whitespace and newlines. Finally, it introduces string constants with C-style escapes.