Hi everyone,
I'm trying to implement a search, where you can enter more than one searchterm to form an AND-Condition. Also it should search in different fields of the database.
So for instance:
You when you enter "Bill Seattle", you should get a record where NAME matches Bill and CITY matches Seattle. You shoudn't get any rows where just the CITY matches Seattle.
Now, this doesn't sound to complicated, but it really seems to be harder than I thought. I've come up with something like this:
SELECT * FROM ADDRESS WHERE
((NAME LIKE 'Bill%') OR (NAME LIKE 'Seattle%'))
AND
((CITY LIKE 'Bill%') OR (CITY LIKE 'Seattle%'))
This works well in our previous case, but suppose we add another field:
SELECT * FROM ADDRESS WHERE
((NAME LIKE 'Bill%') OR (NAME LIKE 'Seattle%'))
AND
((CITY LIKE 'Bill%') OR (CITY LIKE 'Seattle%'))
AND
((COMPANY LIKE 'Bill%') OR (COMPANY LIKE 'Seattle%'))
Now it won't return the desired result. What I essentialy need (I guess) is a way to determine if a sub-condition like
((COMPANY LIKE 'Bill%') OR (COMPANY LIKE 'Seattle%'))
returns something and if not, i need to ignore that condition.
Btw, I'm using MSSQL '05.