views:

22

answers:

2

Hi

I am doing a project in ASP.NET that at one point searches a SQL database for a postcode using Datasets:

string postcode = "%" + searchTerm.Trim().Replace(' ', '%') + "%";
SearchDataSet.SearchCustomerTableDataTable custTable = custAdapter.GetDataCustPostcode(postcode);

The GetDataCustPostcode runs:

SELECT * FROM CustomerTable WHERE (CustomerPostcode LIKE @CustPostcode)

The expected results are returned when I try:

searchTerm = "BT14" searches for a postcode of %BT14%

or searchTerm = "BT14 7" searches for a postcode of %BT14%7%

custTable is empty when I try:

searchTerm = "BT14 7D" searches for a postcode of %BT14%7D%

If I try writing a SQL query directly, i.e. typing:

SELECT * FROM CustomerTable WHERE (CustomerPostcode LIKE '%BT14%7D%')

Then the expected results are returned.

Can anyone advise why this is? Is it something to do with the characters in the string?

Thanks

Clivest

+1  A: 

Consider using the single-character wildcard, the underscore, for mid-string matching, and percents only on the ends of the string.

kbrimington
Thanks for the reply. I thought I had to use a percentage in the middle because the postcode could be `BT147DT` or `BT14 7DT`. Is that right? Or is there another wildcard that would be more appropriate
Clivest
Unfortunately, no. I'm glad, per your comment, that things are working for you. If you are interested in learning more about the different supported wildcards, check this out: http://manuals.sybase.com/onlinebooks/group-asarc/srg1100e/sqlref/@Generic__BookTextView/73386;pt=73946
kbrimington
A: 

%7D is a special character in some circumstances... perhaps this one as well? Does the problem also happen for other combinations?

Tobiasopdenbrouw
Thanks for the reply. It also does it for `BT14 7E`, but not `BT14 9E`
Clivest