Tuesday, March 13, 2012

Searching special character (e.g. %,_) with .NET and Stored Procedure

Today, I spent 2 hours to searching the way to search with "%,_" characters. When my QC fill these characters on search textbox, it has shown all items. How a stupid I am!! :D
Finally, I found the way to fix it. It's quite simple. Below is the steps
Assumption, your text search is "%" (I named it is searchText variable)
          searchText = searchText .Replace("%","[%]"); //same with "_" character
It works right way....
For more information you can see "WildCard Characters" section in this page
LIKE '5[%]' = 5%
LIKE '[_]n' = _n
LIKE '[a-cdf]' = a, b, c, d, or f
LIKE '[-acdf]' = -, a, c, d, or f
LIKE '[ [ ]' = [
LIKE ']' = ]
LIKE 'abc[_]d%' = abc_d and abc_de
LIKE 'abc[def]' = abcd, abce, and abcf

No comments:

Post a Comment