MariaDB BINARY() Function
The MariaDB BINARY() function converts a value to a binary string. This function is equivalent to using CAST(value AS BINARY).
Syntax
/* version 1 */ BINARY(value) /* version 2 */ BINARY value
Parameters
value |
Required. Specify the value to convert to a binary string. |
Return Value
Returns the converted value as a binary string.
Example:
The example below shows the usage of BINARY() function.
SELECT BINARY(123); Result: '123' SELECT BINARY('alphacodingskills.com'); Result: 'alphacodingskills.com' SELECT BINARY('A'); Result: 'A' SELECT BINARY 123; Result: '123' SELECT BINARY 'alphacodingskills.com'; Result: 'alphacodingskills.com' SELECT BINARY 'A'; Result: 'A'
Using BINARY() function to compare strings byte-by-byte
When = operator is used, MariaDB performs a character-by-character comparison of strings.
Example:
In the example below, MariaDB performs a character-by-character comparison of 'HELLO' and 'hello' and return 1 (because on a character-by-character basis, 'HELLO' and 'hello' are equivalent):
SELECT 'HELLO' = 'hello'; Result: 1
To perform a byte-by-byte comparison of strings, the BINARY() function can be used which cast a value to a binary string and forces a byte-by-byte comparison of strings.
Example:
In the example below, MariaDB performs a byte-by-byte comparison of 'HELLO' and 'hello' and return 0 (because on a byte-by-byte basis, 'HELLO' and 'hello' are NOT equivalent):
SELECT BINARY 'HELLO' = 'hello'; Result: 0
❮ MariaDB Functions