ID EN
Cast Functions and Operators

BINARY

MySQL 8.4 🇮🇩 Bahasa Indonesia

BINER expr

Syntax

MYSQL
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

Contoh

Example

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”.

MYSQL
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
Example 2

The BINARY operator is deprecated; you should expect its removal in a future version of MySQL. Use CAST(... AS BINARY) instead.

MYSQL
CONVERT(expr USING BINARY)
CAST(expr AS BINARY)
BINARY expr
Example 3

In a comparison, BINARY affects the entire operation; it can be given before either operand with the same result.

MYSQL
SELECT 'a' = 'A';
1
SELECT _binary 'a' = 'A';
0
Example 4

To convert a string expression to a binary string, these constructs are equivalent:

MYSQL
CHAR(10) BINARY
CHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
Example 5

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:

MYSQL
CHAR(10) CHARACTER SET binary
BINARY(10)

VARCHAR(10) CHARACTER SET binary
VARBINARY(10)

TEXT CHARACTER SET binary
BLOB