BINARY

Use this MySQL function to treat strings in their binary state.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

BINARY string

Explanation

Use this function to treat strings in their binary state. This function is useful for making SQL statements case-sensitive. Notice that the syntax does not call for parentheses.

Examples

SELECT student_id, name_last
FROM students
WHERE BINARY LEFT(UCASE(name_last), 1) <>
   LEFT(name_last, 1);

+------------+-----------+
| student_id | name_last |
+------------+-----------+
|  433302000 | daniels   |
|  434016005 | de Vitto  |
+------------+-----------+

This statement checks for any students whose last name starts with a lowercase letter. To do this, each student's last name is converted to uppercase letters and then the first letter starting from the left is extracted to compare it to the first letter of the last name without case conversion. The results show one record that is probably a typing error and a second that is probably correct. Notice that the BINARY keyword is specified before the comparison is made between the strings and is applied to both strings.