This tutorial explains how to use the MySQL BINARY function with syntax and examples.
The MySQL BINARY function converts a value to a binary string.
The syntax for the BINARY function in MySQL is:
The value to convert to a binary string.
The BINARY function can be used in the following versions of MySQL:
Let's look at some MySQL BINARY function examples and explore how to use the BINARY function in MySQL.
For example:
Using the BINARY function to cast a value to a binary string is one way to force a byte-by-byte comparison of strings, rather than a character-by-character comparison. Let's investigate this further.
For example:
If we ran the example above, MySQL would perform a character-by-character comparison of 'AODBA' and 'AODBA' and return 1 (because on a character-by-character basis, 'AODBA' and 'AODBA' are equivalent).
However, if we modified the example by adding the BINARY function as follows, changing the comparison to byte-by-byte instead of character-by-character:
MySQL would perform a byte-by-byte comparison of 'AODBA' and 'AODBA' and would return 0 (because on a byte-by-byte basis, 'AODBA' and 'AODBA' are NOT equivalent.)