content_image/804266.jpg content_image/804267.jpg
SELECT _latin1 X’4D7953514C’; SELECT _utf8 0x4D7953514C COLLATE utf8_danish_ci; 

The examples use X’ val ’  notation, but 0x val  notation permits introducers as well. For information about introducers, see Section 10.3.8, “Character Set Introducers” . 

In numeric contexts, MySQL treats a hexadecimal literal like a BIGINT UNSIGNED  (64-bit unsigned integer). To ensure numeric treatment of a hexadecimal literal, use it in numeric context. Ways to do this include adding 0 or using CAST(... AS UNSIGNED) . For example, a hexadecimal literal assigned to a user-defined variable is a binary string by default. To assign the value as a number, use it in numeric context: 

mysql> SET @v1 = X’41’; mysql> SET @v2 = X’41’+0; mysql> SET @v3 = CAST(X’41’ AS UNSIGNED); mysql> SELECT @v1, @v2, @v3; 

<img src='content_image/804266.jpg'>

An empty hexadecimal value ( X’’ ) evaluates to a zero-length binary string. Converted to a number, it produces 0: 

mysql> SELECT CHARSET(X’’), LENGTH(X’’); 

<img src='content_image/804267.jpg'>

mysql> SELECT X’’+0; +-------+ | X’’+0 | +-------+ | 0 | +-------+ 

The X’ val ’  notation is based on standard SQL. The 0x  notation is based on ODBC, for which hexadecimal strings are often used to supply values for BLOB  columns. 

To convert a string or a number to a string in hexadecimal format, use the HEX()  function: 

mysql> SELECT HEX(’cat’); +------------+ | HEX(’cat’) | +------------+ | 636174 | +------------+ mysql> SELECT X’636174’; +-----------+ | X’636174’ | +-----------+ | cat | +-----------+ 

For hexadecimal literals, bit operations are considered numeric context, but bit operations permit numeric or binary string arguments in MySQL 8.0 and higher. To explicitly specify binary string context for hexadecimal literals, use a _binary  introducer for at least one of the arguments: 

mysql> SET @v1 = X’000D’ | X’0BC0’; mysql> SET @v2 = _binary X’000D’ | X’0BC0’; 

