<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="charset-introducer">
      </a>
      12.3.8 Character Set Introducers
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045217606656">
  </a>
  <a class="indexterm" name="idm46045217605200">
  </a>
  <p>
   A character string literal, hexadecimal literal, or bit-value
        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>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa58358720"><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> <em class="replaceable">literal</em> <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.
  </p>
  <p>
   For character string literals, space between the introducer and
        the string is permitted but optional.
  </p>
  <p>
   For character set literals, 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>
   . For
        additional discussion and examples, see
   <a class="xref" href="charset-literal.html" title="12.3.6 Character String Literal Character Set and Collation">
    Section 12.3.6, “Character String Literal Character Set and Collation”
   </a>
   .
  </p>
  <p>
   Examples:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa78023428"><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>

<span class="token keyword">SELECT</span> _latin1 X<span class="token string">'4D7953514C'</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> _utf8mb4 <span class="token number">0x4D7953514C</span> <span class="token keyword">COLLATE</span> utf8mb4_danish_ci<span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> _latin1 b<span class="token string">'1000001'</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> _utf8mb4 <span class="token number">0b1000001</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>
   Character string literals can be designated as binary strings by
        using the
   <code class="literal">
    _binary
   </code>
   introducer. Hexadecimal
        literals and bit-value literals are binary strings by default,
        so
   <code class="literal">
    _binary
   </code>
   is permitted, but normally
        unnecessary.
   <code class="literal">
    _binary
   </code>
   may be useful to
        preserve a hexadecimal or bit literal as a binary string in
        contexts for which the literal is otherwise treated as a number.
        For example, bit operations permit numeric or binary string
        arguments in MySQL 8.4 and higher, but treat
        hexadecimal and bit literals as numbers by default. To
        explicitly specify binary string context for such literals, use
        a
   <code class="literal">
    _binary
   </code>
   introducer for at least one of the
        arguments:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa66025281"><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 variable">@v1</span> <span class="token operator">=</span> X<span class="token string">'000D'</span> <span class="token operator">|</span> X<span class="token string">'0BC0'</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SET</span> <span class="token variable">@v2</span> <span class="token operator">=</span> _binary X<span class="token string">'000D'</span> <span class="token operator">|</span> X<span class="token string">'0BC0'</span><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 variable">@v1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">HEX</span><span class="token punctuation">(</span><span class="token variable">@v2</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>
<span class="token output"><span class="token punctuation">|</span> HEX(@v1) <span class="token punctuation">|</span> HEX(@v2) <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>
<span class="token output"><span class="token punctuation">|</span> BCD      <span class="token punctuation">|</span> 0BCD     <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></code></pre>
  </div>
  <p>
   The displayed result appears similar for both bit operations,
        but the result without
   <code class="literal">
    _binary
   </code>
   is a
   <code class="literal">
    BIGINT
   </code>
   value, whereas the result with
   <code class="literal">
    _binary
   </code>
   is a binary string. Due to the
        difference in result types, the displayed values differ:
        High-order 0 digits are not displayed for the numeric result.
  </p>
  <p>
   MySQL determines the character set and collation of a character
        string literal, hexadecimal literal, or bit-value 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:
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         For a character string literal, 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>
         For a hexadecimal literal or bit-value literal, the only
                permitted collation is
         <code class="literal">
          binary
         </code>
         because
                these types of literals are binary strings by default.
        </p>
       </li>
      </ul>
     </div>
    </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):
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         For a character string literal, 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>
       <li class="listitem">
        <p>
         For a hexadecimal literal or bit-value literal, the
                character set and collation are
         <code class="literal">
          binary
         </code>
         .
        </p>
       </li>
      </ul>
     </div>
    </li>
   </ul>
  </div>
  <p>
   Examples:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Nonbinary strings 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 language-sql"><div class="docs-select-all right" id="sa91008324"><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>
<span class="token keyword">SELECT</span> _latin1 X<span class="token string">'0A0D'</span> <span class="token keyword">COLLATE</span> latin1_german1_ci<span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> _latin1 b<span class="token string">'0110'</span> <span class="token keyword">COLLATE</span> latin1_german1_ci<span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      Nonbinary strings 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 language-sql"><div class="docs-select-all right" id="sa39178445"><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>
<span class="token keyword">SELECT</span> _utf8mb4 X<span class="token string">'0A0D'</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> _utf8mb4 b<span class="token string">'0110'</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      Binary strings 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 language-sql"><div class="docs-select-all right" id="sa39233732"><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>
<span class="token keyword">SELECT</span> X<span class="token string">'0A0D'</span><span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> b<span class="token string">'0110'</span><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      The hexadecimal literal and bit-value literal need no
            introducer because they are binary strings by default.
     </p>
    </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="sa13055279"><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>
     <p>
      This construction (
      <code class="literal">
       COLLATE
      </code>
      only) does not
            work for hexadecimal literals or bit literals because their
            character set is
      <code class="literal">
       binary
      </code>
      no matter the
            connection character set, and
      <code class="literal">
       binary
      </code>
      is
            not compatible with the
      <code class="literal">
       utf8mb4_0900_ai_ci
      </code>
      collation. The only
            permitted
      <code class="literal">
       COLLATE
      </code>
      clause in the absence
            of an introducer is
      <code class="literal">
       COLLATE binary
      </code>
      .
     </p>
    </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="sa12154941"><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>
 </div>
 <br/>
</div>
