Click here to Skip to main content
15,924,367 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all,

i have one textbox like search box..
how to write a query for search data like city,state
SQL
TableA
------
Address1
Address2
City
State
Zip

All the above columns in the table are full text indexed. Let's say if the user enters "123 Apple street FL 33647" and I have a record in the table as

SQL
 City = "Tampa", State = "FL". I would like the query to return this.am enter into the text in search box like Tampa,FL. but i did not get any records. my query like this 
CITY LIKE '%'+@SERCHTEXT OR CITY LIKE '%'+@SERCHTEXT+'%' OR CITY LIKE @SERCHTEXT+'%'  
OR
[STATE] LIKE '%'+@SERCHTEXT OR [STATE] LIKE '%'+@SERCHTEXT+'%' OR [STATE] LIKE @SERCHTEXT+'%'

when enter the city or state only i got the correct output

can you please let me know how I would do this.

thanking you
Posted
Updated 19-Oct-12 3:56am
v5

use the like keyword in SQL or String.Contain keyword in C# also you can use the lambda expression or Linq query here.

Thanks,
Ambesha
 
Share this answer
 
http://msdn.microsoft.com/en-us/library/ms142488(v=sql.105).aspx[^]

or you can try like this :-

SQL
Select * from TableA where city like @Str1 or state like @Str1 or country like @str3
 
Share this answer
 
v2
You need to very smart and aware while writing code for this

algo will be like this
1. Get the address string
2. slipt the string by whitespace
3. create a query
4. pass the slipted string members to where clause of your query .

I believe your query contains AND condition in the where clause. the state 'TEMPA' is not provided by the user hence it will fail. i will suggest you to create dynamic query string depending upon what user had provided.
 
Share this answer
 
Comments
Santhosh23 19-Oct-12 8:44am    
how to write query for slipt..?
you can make use of like operator

SQL
SELECT col1,col2,col3 FROM Addresstable WHERE State LIKE '%' + @state + '%' and
City LIKE '%' + @city + '%'


Here
SQL
@state 
and
@city
are parameters being passed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900