Split string and use with LIKE keyword in SQL Server
hi all,
Here i want to split the CustNm values from table with spaces and search with each word. how to do that? If i type" ba" in textbox it should display only values starting with ba
[WebMethod]
public static List<string> GetAutoCompleteDetails(string custdetails, string city)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=SHAAN\\SQLEXPRESS;
Initial Catalog=DataPublishers;
User ID=sa;
Password=admin123")) {
using (SqlCommand cmd = new SqlCommand("select distinct CustNm from DP_details Where CustNm like @DetailsText+'%' and ADD3 = @City", con)) {
con.Open();
cmd.Parameters.AddWithValue("@DetailsText", custdetails);
cmd.Parameters.AddWithValue("@City", city);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
result.Add(dr["CustNm"].ToString());
}
return result;
}
}
}
eg
Bakers
Home Bakers
State bank Of India
Not Abad Homes (This should not list)
How to change the query?
Thanks & Regards
Neethu