How do I escape a string in SQL Server's stored procedure so that it is safe to use in LIKE expression.
Suppose I have an NVARCHAR variable like so:
declare @myString NVARCHAR(100);
And I want to use it in a LIKE expression:
... WHERE ... LIKE '%' + @myString + '%';
How do I escape the string (more specifically, characters that are meaningful to LIKE pattern matching, e.g. '%' or '?') in T-SQL, so that it is safe to use in this manner?
For example: given @myString = 'aa%bb' I want WHERE ... LIKE '%' + @somehowEscapedMyString + '%' to match 'aa%bb', 'caa%bbc' but not 'aaxbb' or 'caaxbb'.