How does the Like predicate work in SQL?

2019-07-23 07:17发布

问题:

I have a field called OrderNumber and there's already a record with that field value of "JY8023".

I tried querying using this SQL code, but it returned nothing.

SELECT .... WHERE OrderNumber LIKE "JY8023"

I also tried using wildcards and it worked

SELECT .... WHERE OrderNumber Like "%JY8023%"

So does that mean OrderNumber Like "JY9023" is not the same as OrderNumber = "JY8023"?

回答1:

the string has characters before or after it, that you can't see. try something like select length(OrderNumber) WHERE OrderNumber Like "%JY8023%" to confirm this. Some characters are not only invisible, but unselectable with a cursor. But, they're there and they affect string comparisons.

additional debugging steps to follow will be to use substring to extract the offending part, and other string functions to further inspect the value. like, maybe selecting the string as a hex encoded string will help you identify the bytes.



标签: sql db2