I want to check whether a column has any values that are a single space character.
I initially thought that
WHERE my_column = ' '
would be sensible. But no. That will also match columns which have multiple spaces for some reason:
SELECT ' ' = ' ' => true
So I can use a regular express or hex encoding to test:
WHERE HEX(my_column) = '20'
WHERE my_column REGEXP '^\ $'
Both work. But I suspect both (certainly the latter) will be quite inefficient.
Is there a better way?
A
BINARYcomparison of the two strings is required for an exact matchUnder normal circumstances, trailing whitespace is not regarded in the comparison, but the
BINARYoperator forces it to be:Incidentally, it isn’t just whitespace-only comparisons that are affected by the trailing whitespace issue:
…but…
…and even more confusingly, leading whitespace is significant:
Regarding indexing:
BINARYwill prevent an index from being used on the character column. However, a note on the docs suggests that the index will be used if theBINARYoperator is applied to the string literal side of the comparison as in: