I have a table in Sql Server 2000 called Table1with a few columns, one of the columns is varchar(255), called ‘Col5’.
The contents if one of the rows in this column is [test].
When I try to test for a part of this string using the LIKE operator, I cannot search for the [ in any way.
This is what I get:
1- trying SELECT * FROM Table1 WHERE Col1 LIKE '%[%' returns NO results.
2- trying SELECT * FROM Table1 WHERE Col1 LIKE '%[t%' returns NO results.
3- trying SELECT * FROM Table1 WHERE Col1 LIKE '%t]%' returns 1 results which as expected.
4- trying SELECT * FROM Table1 WHERE Col1 LIKE '%test%' returns 1 results which as expected.
This is a strange behaviour, is it a bug, what’s going on, any ideas?
That character has special meaning in the pattern syntax and needs to be escaped either by defining an explicit escape character
or by using more square brackets
In addition to its role in escaping characters it is used in the syntax when defining ranges or sets of characters to match. e.g.
LIKE '[0-9]%'to find all values starting with a digit.When the range or set is not closed off with a
]it appears as though SQL Server just adds one on to the end. Soc like '%[%'is treated asc like '%[%]'and finds all values containing the%character.