I am trying to filter items with a stored procedure using like. The column is a varchar(15). The items I am trying to filter have square brackets in the name.
For ex
Use Following.
For user input to search as it is, use escape, in that it will require following replacement for all special characters (below covers all of SQL Server).
Here single quote "'" is not taken as it does not affect like clause as It is a matter of string concatenation.
"-" & "^" & "]" replace is not required as we are escaping "[".
String FormattedString = "UserString".Replace("ð","ðð").Replace("_", "ð_").Replace("%", "ð%").Replace("[", "ð[");
Then, in SQL Query it should be as following. (In parameterised query, string can be added with patterns after above replacement).
To search exact string.
like 'FormattedString' ESCAPE 'ð'
To search start with string
like '%FormattedString' ESCAPE 'ð'
To search end with string
like 'FormattedString%' ESCAPE 'ð'
To search contain with string
like '%FormattedString%' ESCAPE 'ð'
and so on for other pattern matching. But direct user input needs to format as mentioned above.