Skip to main content
Version: V1.4.0

BINARY conversion operators

seekdb supports the BINARY conversion operator.

Syntax of the BINARY operator​

The syntax of the BINARY operator is as follows:

BINARY expr

The BINARY operator converts the expression expr to a binary string (a string with the binary character set and binary collation). In string comparisons, BINARY is often used to compare the binary values of strings rather than character by character, and trailing spaces are considered in the comparison. Here is an example:

SELECT 'aa' = 'AA';
+-------------+
| 'aa' = 'AA' |
+-------------+
| 1 |
+-------------+
1 row in set (0.001 sec)

SELECT BINARY 'aa' = 'AA';
+--------------------+
| BINARY 'aa' = 'AA' |
+--------------------+
| 0 |
+--------------------+
1 row in set (0.001 sec)

SELECT 'aa' = 'aa ';
+--------------+
| 'aa' = 'aa ' |
+--------------+
| 1 |
+--------------+
1 row in set (0.000 sec)

SELECT BINARY 'aa' = 'aa ';
+---------------------+
| BINARY 'aa' = 'aa ' |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.000 sec)

Notes on binary string conversion​

In seekdb, you can convert a string expression to a binary string using the following three methods:

CONVERT(expr USING BINARY)
CAST(expr AS BINARY)
BINARY expr

The BINARY operator in an expression has a different effect from the BINARY attribute in a character column definition. For a column defined with the BINARY attribute, the database assigns the table's default character set and that character set's binary (_bin) collation. For example, if the table's default character set is utf8mb4, the following definitions are equivalent:

CHAR(5) BINARY
<=>
CHAR(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin

Using CHARACTER SET binary in the definition of a CHAR, VARCHAR, or TEXT column causes the column to be treated as a binary string data type. The following example demonstrates this:

CHAR(5) CHARACTER SET binary
<=>
BINARY(5)

VARCHAR(10) CHARACTER SET binary
<=>
VARBINARY(10)

TEXT CHARACTER SET binary
<=>
BLOB