BINARY expr
SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;
OK
SELECT 'a' = 'A';
1
SELECT BINARY 'a' = 'A';
0
SELECT 'a' = 'a ';
1
SELECT BINARY 'a' = 'a ';
0
The BINARY operator converts the expression to a binary string (a string that has the binary character set and binary collation). A common use for BINARY is to force a character string comparison to be done byte by byte using numeric byte values rather than character by character. The BINARY operator also causes trailing spaces in comparisons to be significant. For information about the differences between the binary collation of the binary character set and the _bin collations of nonbinary character sets, see Section 12.8.5, “The binary Collation Compared to _bin Collations”.
SET NAMES utf8mb4 COLLATE utf8mb4_general_ci;
OK
SELECT 'a' = 'A';
1
SELECT BINARY 'a' = 'A';
0
SELECT 'a' = 'a ';
1
SELECT BINARY 'a' = 'a ';
0
The BINARY operator is deprecated; you should expect its removal in a future version of MySQL. Use CAST(... AS BINARY) instead.
CONVERT(expr USING BINARY)
CAST(expr AS BINARY)
BINARY expr
In a comparison, BINARY affects the entire operation; it can be given before either operand with the same result.
SELECT 'a' = 'A';
1
SELECT _binary 'a' = 'A';
0
To convert a string expression to a binary string, these constructs are equivalent:
CHAR(10) BINARY
CHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
If a value is a string literal, it can be designated as a binary string without converting it by using the _binary character set introducer:
CHAR(10) CHARACTER SET binary
BINARY(10)
VARCHAR(10) CHARACTER SET binary
VARBINARY(10)
TEXT CHARACTER SET binary
BLOB