I’m trying to achieve something that Im not sure how to do it. I’m working on a website based on ASP Classic and SQL 2008, IIS7. In search page I have option to have couple value per query like default.asp?q=one|two|three&q2=four|five|six .
What Im trying to do is to use wildcard in case my queries dont have value and have ability to search couple word in one column at the same time in case my query has couple value. Here are what I want to do:
- If query has value then go fine that in database (Which is pretty simple)
- If query has couple value separated with “|” sign then split them and search for all of them.
- If query does not have value at all, then use wildcard and do not search
for anything.
So to achieve this I’ve tried couple thing but didn’t get any success. Here is the example of my code.
One ———–
@style nVarchar(150) = ''
Select * FROM mytable WHERE CONTAINS(style, @style) AND ...
I can manage to split the query to something like '"* value1 *" OR "* value2 *"' to get the best result out of CONTAINS but This one is working if “style” has value. If style does not have value there is no wildcard so I can retrive all data without filtering down the database.
Two —————
@style nVarchar(150) = ''
Select * FROM mytable WHERE style LIKE '%' + @style + '%' AND ...
With LIKE clause I can use wildcard like '%' in case style does not have any value but I cannot search couple value in the same column.
So, keep in mind I’m using “Stored Procedure” since this is kind of complex and I need to use this in couple places. I will have have 10-12 query for this search.
Any Idea will help me to get this going.
I understand your problem is with values separated by “|”. In SQL 2008 you can use Xml functions to transform the string into a table and query it against your base table:
Does it help?