MySQL BINARY Function
The BINARY()
function in MySQL converts a given value into a binary string representation. While it might seem specialized, it has important implications for how MySQL performs string comparisons.
BINARY(): Definition and Usage
By default, MySQL performs case-insensitive string comparisons. This means that "hello" and "HELLO" are considered equal. However, using BINARY()
forces MySQL to perform a byte-by-byte comparison, making the comparison case-sensitive. This function is equivalent to CAST(value AS BINARY)
.
Syntax
Syntax
BINARY value
Parameter Values
Parameter | Description |
---|---|
value |
The value you want to convert to a binary string. This is required. |
Examples
Converting a String to Binary
This converts the string "W3Schools.com" to a binary string representation.
Syntax
SELECT BINARY "W3Schools.com";
Output
(A binary string representation of "W3Schools.com" will be displayed here)
Case-Sensitive String Comparison (Without BINARY)
Demonstrates MySQL's default case-insensitive comparison.
Syntax
SELECT "HELLO" = "hello";
Output
1 (True)
Case-Sensitive String Comparison (With BINARY)
Using BINARY
enforces a case-sensitive comparison.
Syntax
SELECT BINARY "HELLO" = "hello";
Output
0 (False)