<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="charset-literal">
      </a>
      12.3.6 Character String Literal Character Set and Collation
     </h3>
    </div>
   </div>
  </div>
  <p>
   Every character string literal has a character set and a
        collation.
  </p>
  <p>
   For the simple statement
   <code class="literal">
    SELECT
        '
    <em class="replaceable">
     <code>
      string
     </code>
    </em>
    '
   </code>
   , the string has
        the connection default character set and collation defined by
        the
   <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
    <code class="literal">
     character_set_connection
    </code>
   </a>
   and
   <a class="link" href="server-system-variables.html#sysvar_collation_connection">
    <code class="literal">
     collation_connection
    </code>
   </a>
   system
        variables.
  </p>
  <p>
   A character string literal may have an optional character set
        introducer and
   <code class="literal">
    COLLATE
   </code>
   clause, to designate
        it as a string that uses a particular character set and
        collation:
  </p>
  <a class="indexterm" name="idm46045217702352">
  </a>
  <a class="indexterm" name="idm46045217700864">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa78815256"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token punctuation">[</span>_<em class="replaceable">charset_name</em><span class="token punctuation">]</span><span class="token string">'<em class="replaceable">string</em>'</span> <span class="token punctuation">[</span><span class="token keyword">COLLATE</span> <em class="replaceable">collation_name</em><span class="token punctuation">]</span></code></pre>
  </div>
  <p>
   The
   <code class="literal">
    _
    <em class="replaceable">
     <code>
      charset_name
     </code>
    </em>
   </code>
   expression is formally called an
   <span class="emphasis">
    <em>
     introducer
    </em>
   </span>
   . It tells the parser,
   <span class="quote">
    “
    <span class="quote">
     the
        string that follows uses character set
     <em class="replaceable">
      <code>
       charset_name
      </code>
     </em>
     .
    </span>
    ”
   </span>
   An introducer
        does not change the string to the introducer character set like
   <a class="link" href="cast-functions.html#function_convert">
    <code class="literal">
     CONVERT()
    </code>
   </a>
   would do. It does not
        change the string value, although padding may occur. The
        introducer is just a signal. See
   <a class="xref" href="charset-introducer.html" title="12.3.8 Character Set Introducers">
    Section 12.3.8, “Character Set Introducers”
   </a>
   .
  </p>
  <p>
   Examples:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa87892012"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token string">'abc'</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> _latin1<span class="token string">'abc'</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> _binary<span class="token string">'abc'</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> _utf8mb4<span class="token string">'abc'</span> <span class="token keyword">COLLATE</span> utf8mb4_danish_ci<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Character set introducers and the
   <code class="literal">
    COLLATE
   </code>
   clause are implemented according to standard SQL specifications.
  </p>
  <p>
   MySQL determines the character set and collation of a character
        string literal in the following manner:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If both
      <em class="replaceable">
       <code>
        _charset_name
       </code>
      </em>
      and
      <code class="literal">
       COLLATE
       <em class="replaceable">
        <code>
         collation_name
        </code>
       </em>
      </code>
      are
            specified, character set
      <em class="replaceable">
       <code>
        charset_name
       </code>
      </em>
      and collation
      <em class="replaceable">
       <code>
        collation_name
       </code>
      </em>
      are used.
      <em class="replaceable">
       <code>
        collation_name
       </code>
      </em>
      must be a
            permitted collation for
      <em class="replaceable">
       <code>
        charset_name
       </code>
      </em>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      If
      <em class="replaceable">
       <code>
        _charset_name
       </code>
      </em>
      is specified but
      <code class="literal">
       COLLATE
      </code>
      is not specified, character set
      <em class="replaceable">
       <code>
        charset_name
       </code>
      </em>
      and its default
            collation are used. To see the default collation for each
            character set, use the
      <a class="link" href="show-character-set.html" title="15.7.7.4 SHOW CHARACTER SET Statement">
       <code class="literal">
        SHOW CHARACTER
            SET
       </code>
      </a>
      statement or query the
      <code class="literal">
       INFORMATION_SCHEMA
      </code>
      <a class="link" href="information-schema-character-sets-table.html" title="28.3.4 The INFORMATION_SCHEMA CHARACTER_SETS Table">
       <code class="literal">
        CHARACTER_SETS
       </code>
      </a>
      table.
     </p>
    </li>
    <li class="listitem">
     <p>
      If
      <em class="replaceable">
       <code>
        _charset_name
       </code>
      </em>
      is not specified
            but
      <code class="literal">
       COLLATE
       <em class="replaceable">
        <code>
         collation_name
        </code>
       </em>
      </code>
      is
            specified, the connection default character set given by the
      <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
       <code class="literal">
        character_set_connection
       </code>
      </a>
      system variable and collation
      <em class="replaceable">
       <code>
        collation_name
       </code>
      </em>
      are used.
      <em class="replaceable">
       <code>
        collation_name
       </code>
      </em>
      must be a
            permitted collation for the connection default character
            set.
     </p>
    </li>
    <li class="listitem">
     <p>
      Otherwise (neither
      <em class="replaceable">
       <code>
        _charset_name
       </code>
      </em>
      nor
      <code class="literal">
       COLLATE
       <em class="replaceable">
        <code>
         collation_name
        </code>
       </em>
      </code>
      is
            specified), the connection default character set and
            collation given by the
      <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
       <code class="literal">
        character_set_connection
       </code>
      </a>
      and
      <a class="link" href="server-system-variables.html#sysvar_collation_connection">
       <code class="literal">
        collation_connection
       </code>
      </a>
      system variables are used.
     </p>
    </li>
   </ul>
  </div>
  <p>
   Examples:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      A nonbinary string with
      <code class="literal">
       latin1
      </code>
      character
            set and
      <code class="literal">
       latin1_german1_ci
      </code>
      collation:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa29226320"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> _latin1<span class="token string">'Müller'</span> <span class="token keyword">COLLATE</span> latin1_german1_ci<span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      A nonbinary string with
      <code class="literal">
       utf8mb4
      </code>
      character
            set and its default collation (that is,
      <code class="literal">
       utf8mb4_0900_ai_ci
      </code>
      ):
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa35806966"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> _utf8mb4<span class="token string">'Müller'</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      A binary string with
      <code class="literal">
       binary
      </code>
      character set
            and its default collation (that is,
      <code class="literal">
       binary
      </code>
      ):
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa91366553"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> _binary<span class="token string">'Müller'</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      A nonbinary string with the connection default character set
            and
      <code class="literal">
       utf8mb4_0900_ai_ci
      </code>
      collation (fails
            if the connection character set is not
      <code class="literal">
       utf8mb4
      </code>
      ):
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa12154981"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token string">'Müller'</span> <span class="token keyword">COLLATE</span> utf8mb4_0900_ai_ci<span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      A string with the connection default character set and
            collation:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa19823620"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token string">'Müller'</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
   </ul>
  </div>
  <p>
   An introducer indicates the character set for the following
        string, but does not change how the parser performs escape
        processing within the string. Escapes are always interpreted by
        the parser according to the character set given by
   <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
    <code class="literal">
     character_set_connection
    </code>
   </a>
   .
  </p>
  <p>
   The following examples show that escape processing occurs using
   <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
    <code class="literal">
     character_set_connection
    </code>
   </a>
   even
        in the presence of an introducer. The examples use
   <a class="link" href="set-names.html" title="15.7.6.3 SET NAMES Statement">
    <code class="literal">
     SET NAMES
    </code>
   </a>
   (which changes
   <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
    <code class="literal">
     character_set_connection
    </code>
   </a>
   , as
        discussed in
   <a class="xref" href="charset-connection.html" title="12.4 Connection Character Sets and Collations">
    Section 12.4, “Connection Character Sets and Collations”
   </a>
   ), and display
        the resulting strings using the
   <a class="link" href="string-functions.html#function_hex">
    <code class="literal">
     HEX()
    </code>
   </a>
   function so that the exact
        string contents can be seen.
  </p>
  <p>
   Example 1:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa38895921"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> <span class="token keyword">NAMES</span> latin1<span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">HEX</span><span class="token punctuation">(</span><span class="token string">'à\n'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">HEX</span><span class="token punctuation">(</span>_sjis<span class="token string">'à\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> HEX('à\n')  <span class="token punctuation">|</span> HEX(_sjis'à\n')  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> E00A       <span class="token punctuation">|</span> E00A            <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   Here,
   <code class="literal">
    à
   </code>
   (hexadecimal value
   <code class="literal">
    E0
   </code>
   ) is followed by
   <code class="literal">
    \n
   </code>
   , the
        escape sequence for newline. The escape sequence is interpreted
        using the
   <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
    <code class="literal">
     character_set_connection
    </code>
   </a>
   value
        of
   <code class="literal">
    latin1
   </code>
   to produce a literal newline
        (hexadecimal value
   <code class="literal">
    0A
   </code>
   ). This happens even for
        the second string. That is, the
   <code class="literal">
    _sjis
   </code>
   introducer does not affect the parser's escape processing.
  </p>
  <p>
   Example 2:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa90261611"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> <span class="token keyword">NAMES</span> sjis<span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token function">HEX</span><span class="token punctuation">(</span><span class="token string">'à\n'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">HEX</span><span class="token punctuation">(</span>_latin1<span class="token string">'à\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> HEX('à\n')  <span class="token punctuation">|</span> HEX(_latin1'à\n')  <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> E05C6E     <span class="token punctuation">|</span> E05C6E            <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
  </div>
  <p>
   Here,
   <a class="link" href="server-system-variables.html#sysvar_character_set_connection">
    <code class="literal">
     character_set_connection
    </code>
   </a>
   is
   <code class="literal">
    sjis
   </code>
   , a character set in which the
        sequence of
   <code class="literal">
    à
   </code>
   followed by
   <code class="literal">
    \
   </code>
   (hexadecimal values
   <code class="literal">
    05
   </code>
   and
   <code class="literal">
    5C
   </code>
   ) is a valid multibyte character.
        Hence, the first two bytes of the string are interpreted as a
        single
   <code class="literal">
    sjis
   </code>
   character, and the
   <code class="literal">
    \
   </code>
   is not interpreted as an escape character.
        The following
   <code class="literal">
    n
   </code>
   (hexadecimal value
   <code class="literal">
    6E
   </code>
   ) is not interpreted as part of an escape
        sequence. This is true even for the second string; the
   <code class="literal">
    _latin1
   </code>
   introducer does not affect escape
        processing.
  </p>
 </div>
 <br/>
</div>
