The LIKE operator in a SQL server query can be very useful to match custom patterns. However sometimes the need raises to escape some characters or substrings from the pattern, such as ampersands '%', underscores '_', square brackets '[' and ']', etc.
Indeed I'm using parametrized queries but it does not solve LIKE case because for example searching for _
would mean "any character".
What is the set of characters that must be considered while escaping such patterns? Can a C# function be provided to perform a safe escape?
%, _, [, ], and ^ need to be escaped, and you will need to choose a suitable escape character, i.e. one that you aren't using elsewhere in your LIKE pattern.
Full description here LIKE (Transact-SQL)
I'm sure you could write a function in C# to do that.