MySQL: BINARY Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL BINARY Function

In this guide, we will explain how to use the MySQL BINARY function with syntax and examples.

Description

The MySQL BINARY function converts a value to a binary string.

Syntax

The syntax for the BINARY function in MySQL is:

BINARY value

Parameters or Arguments

value

The value to convert to a binary string.

Note

  • The BINARY function is shorthand for CAST(value AS BINARY).
  • See also CONVERT function.

Applies To

The BINARY function can be used in the following versions :

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23.3

Example

Let’s look at someBINARY function examples and explore how to use the BINARY function.

For example:

mysql> SELECT BINARY('adglob.in'); 
Result: 'adglob.in'

mysql> SELECT BINARY('T'); 
Result: 'T'

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:

mysql> SELECT 'ADGLOB' = 'adglob';
Result: 1

If we ran the example above, MySQL would perform a character-by-character comparison of ‘ADGLOB’ and ‘adglob’ and return 1 (because on a character-by-character basis, ‘ADGLOB’ and ‘adglob’ 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> SELECT BINARY 'ADGLOB' = 'adglob';
Result: 0

MySQL would perform a byte-by-byte comparison of ‘ADGLOB’ and ‘adglob’ and would return 0 (because on a byte-by-byte basis, ‘ADGLOB’ and ‘adglob’ are NOT equivalent.)

Next Topic : Click Here

This Post Has One Comment

Leave a Reply