If you get warning number 1287 that reads ‘BINARY expr’ is deprecated and will be removed in a future release. Please use CAST instead when running a query in MySQL, it’s because you’re using the BINARY
operator.
The BINARY
operator is deprecated as of MySQL 8.0.27.
To fix the issue, cast the value to binary using the CAST()
function instead.
Example of Warning
Here’s an example of code that produces the warning:
SELECT BINARY 'Cat';
Result:
+----------------------------+ | BINARY 'Cat' | +----------------------------+ | 0x436174 | +----------------------------+ 1 row in set, 1 warning (0.00 sec)
We can see that it worked OK, but we also got a warning.
I ran that in MySQL 8.0.27 and so I got the warning. If you run it in an earlier version of MySQL, you probably won’t get the warning.
Let’s check the warning:
SHOW WARNINGS;
Result:
+---------+------+----------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------------------------------------------------------------+ | Warning | 1287 | 'BINARY expr' is deprecated and will be removed in a future release. Please use CAST instead | +---------+------+----------------------------------------------------------------------------------------------+
This is in line with the MySQL 8.0.27 release notes which explain:
The
BINARY
operator is now deprecated, and subject to removal in a future release of MySQL. Use ofBINARY
now causes a warning. UseCAST(... AS BINARY)
instead.
Solution
As the warning message alludes to, we can get rid of the warning by using the CAST()
function instead of the BINARY
operator:
SELECT CAST('Cat' AS BINARY);
Result:
+----------------------------------------------+ | CAST('Cat' AS BINARY) | +----------------------------------------------+ | 0x436174 | +----------------------------------------------+ 1 row in set (0.00 sec)
The cat has now been converted to binary without producing any warnings.
You can alternatively use CONVERT()
instead of CAST()
:
SELECT CONVERT('Cat' USING BINARY);
Result:
+----------------------------------------------------------+ | CONVERT('Cat' USING BINARY) | +----------------------------------------------------------+ | 0x436174 | +----------------------------------------------------------+ 1 row in set (0.00 sec)
Same result.