Connecting Tech Pros Worldwide Forums | Help | Site Map

SQL exact search

colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#1: Oct 15 '09
I have the following code that works very well, the problem is that of i search for say rap it returns back results that contain the words telegraph, grapfruit etc, how can i get it to just return results that have the word rap in the search string anywhere??

Expand|Select|Wrap|Line Numbers
  1. <%
  2. Dim strSearch, myarray, strSQL, strSQLExtra 
  3. If Not Request.Form("search") = "" Then 
  4. session("search") = Request.Form("search") 
  5. session("choice") = Request.Form("choice") 
  6. session("queryterm") = session("queryterm")+" "+Request.Form("search")
  7. End If 
  8.  
  9. 'Loop is now in a function, so that we can feed it each field name. 
  10. Function addparams(fieldname,wordArray)  
  11. Dim tmp 
  12. tmp = "" 
  13.     For each item in wordArray 
  14.     tmp = tmp & " (" & fieldname & " Like '%" & item & "%') " & session("choice") & " " 
  15.     next 
  16.     tmp = left(tmp, len(tmp)-4) ' Chops off the last "AND " 
  17.     addparams = tmp 
  18. End Function 
  19.  
  20. 'Build the word array 
  21. myarray = split(session("search"), " ") 
  22.  
  23. 'Build the SQL  
  24. strSQL = "SELECT * FROM mp3 INNER JOIN celebs ON mp3.celebid = celebs.idnumber WHERE (" 
  25.  
  26. strSQL = strSQL & addparams("filename", myarray) ' change field one! 
  27.  
  28. strSQL = strSQL & ") OR (" 
  29.  
  30. strSQL = strSQL & addparams("mp3type", myarray) ' change field two! 
  31.  
  32. strSQL = strSQL & ")order by surname, firstname, filename;" 
  33. session("sqlset") = strSQL
  34.  
  35.   %>

CroCrew's Avatar
Expert
 
Join Date: Jan 2008
Location: Michigan
Posts: 338
#2: Oct 15 '09

re: SQL exact search


Hello colinod,

Here is an example that does what your looking to do.

Expand|Select|Wrap|Line Numbers
  1. <%
  2.     Function WordFinder(FullString, WordToFind)   
  3.         myarray = split(FullString)
  4.         For each item in myarray  
  5.             If (item = WordToFind) Then
  6.                 WordFinder = "Found!"
  7.                 Exit Function
  8.             End If
  9.         next  
  10.         WordFinder = "Not Found."
  11.     End Function  
  12. %>
  13. <html>
  14.     <head>
  15.         <title>Test Page</title>
  16.     </head>
  17.     <body>
  18.         <%
  19.             If (Request.ServerVariables("REQUEST_METHOD") = "POST") Then
  20.                 Response.Write("Looking for [" & Request.Form("xFind") & "]<br />")
  21.                 Response.Write("In [" & Request.Form("xString") & "]<br />")
  22.                 Response.Write(WordFinder(Request.Form("xString"), Request.Form("xFind")))
  23.             End If
  24.         %>
  25.  
  26.         <form method="POST" name="xForm" action="Index.asp">
  27.             String: <input type="text" name="xString"><br />
  28.             Find this: <input type="text" name="xFind"><br />
  29.             <input type="Submit" name="xSubmit" value="Find">
  30.         </form>
  31.     </body>
  32. </html>
  33.  
Hope it helps,
CroCrew~
CroCrew's Avatar
Expert
 
Join Date: Jan 2008
Location: Michigan
Posts: 338
#3: Oct 15 '09

re: SQL exact search


in the first box type in "Hello how are you today Michelle" and the second box type in "to". There should be no matches.

then for the second test:

in the first box type in "Hello how are you today Michelle" and the second box type in "today". There should be a match now.

Hope that that helped you out,
CroCrew~
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#4: Oct 15 '09

re: SQL exact search


Hi thanks for that but i am trying to do this with sql in an asp page, am i in the wrong place for this question?
CroCrew's Avatar
Expert
 
Join Date: Jan 2008
Location: Michigan
Posts: 338
#5: Oct 15 '09

re: SQL exact search


Hello Colinod,

I think you'd have to search for '% rap %' and include the spaces in the search. Like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM [table] WHERE (([field] LIKE '% rap %') OR ([field] LIKE '% rap%') OR ([field] LIKE '%rap %'))
  2.  
Hope that helps,
CroCrew~
Reply