0

I didn't find a similar question maybe because I don't find the correct words (English is not my first language)

Problem

I have a varchar value that has a blank space at the end: "opt-193-381-markets " and when I do a SELECT with the value without the blank space, it returns the same result. It's like MySQL is trimming the value in the WHERE

Table definition:

mysql> desc message;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| FileId     | int(11)      | NO   | PRI | 0       |       |
| MessageKey | varchar(100) | NO   | PRI |         |       |
| NumericKey | tinyint(1)   | NO   |     | 0       |       |
| IsObsolete | tinyint(4)   | YES  | MUL | NULL    |       |
| notes      | text         | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+

Examples:

mysql> SELECT * FROM message WHERE MessageKey = "opt-193-381-markets";
+--------+----------------------+------------+------------+-------+
| FileId | MessageKey           | NumericKey | IsObsolete | notes |
+--------+----------------------+------------+------------+-------+
|    304 | opt-193-381-markets  |          0 |          1 | NULL  |
|    337 | opt-193-381-markets  |          0 |          1 | NULL  |
+--------+----------------------+------------+------------+-------+

Notice that the MessageKey has the value with the blank space at the end and I just asked for the value without that blank space.

mysql> SELECT * FROM message WHERE MessageKey = "opt-193-381-markets ";
+--------+----------------------+------------+------------+-------+
| FileId | MessageKey           | NumericKey | IsObsolete | notes |
+--------+----------------------+------------+------------+-------+
|    304 | opt-193-381-markets  |          0 |          1 | NULL  |
|    337 | opt-193-381-markets  |          0 |          1 | NULL  |
+--------+----------------------+------------+------------+-------+

Adding one more space

mysql> SELECT * FROM message WHERE MessageKey = "opt-193-381-markets  ";
+--------+----------------------+------------+------------+-------+
| FileId | MessageKey           | NumericKey | IsObsolete | notes |
+--------+----------------------+------------+------------+-------+
|    304 | opt-193-381-markets  |          0 |          1 | NULL  |
|    337 | opt-193-381-markets  |          0 |          1 | NULL  |
+--------+----------------------+------------+------------+-------+

Escaping the spaces

mysql> SELECT * FROM message WHERE MessageKey = "opt-193-381-markets\ \ ";
+--------+----------------------+------------+------------+-------+
| FileId | MessageKey           | NumericKey | IsObsolete | notes |
+--------+----------------------+------------+------------+-------+
|    304 | opt-193-381-markets  |          0 |          1 | NULL  |
|    337 | opt-193-381-markets  |          0 |          1 | NULL  |
+--------+----------------------+------------+------------+-------+

I've also tried with single quotes 'opt-193-381-markets'

Question

Why I cannot search for the exact value of MessageKey?

Thanks

Pablo Díaz Ogni
  • 1,033
  • 8
  • 12

1 Answers1

4

Use the binary comparison operator .

SELECT * FROM message WHERE BINARY(MessageKey) = 'opt-193-381-markets';

Pablo Díaz Ogni
  • 1,033
  • 8
  • 12
BuysDB
  • 143
  • 8