I built an app years ago using this; what I did is the front-end gives you a text box to type your terms and it has a drop-down to select between a few options like so:
<option selected value="AND">All of the words entered</option>
<option value="OR">Any of the words entered</option>
<option value="ALL">The exact phrase entered</option>
So I don't want them typing ands or ors - I do that for them. I do let them quote a sub-phrase.
Below is some code that accomplishes this. The resulting phrase is sent as a parameter to a procedure; as the commenter above mentioned you really must do this to protect against SQL injection. Unfortunately this code is legacy ASP VBScript and isn't even well-written by that language's standards but maybe this gives you an idea.
function formatContainsString (sCriteria, sANDorOR)
dim sReturnBuf 'where we build our string
dim sWordList 'sCriteria split
dim lCurPos 'where we are at in sWordList
dim bInnerQuotes ' an open quote was found
if sCriteria = "" or sANDorOR = "" then
formatContainsString = ""
exit function
end if
' If the passed parameter is 'ALL' then use the exact phrase typed by users
if sANDorOR = "ALL" then
formatContainsString = "'" & chr(34) & sCriteria & chr(34) & "'"
Exit Function
End If
sReturnBuf = "'"
sWordList = split(sCriteria," ")
for lCurPos = 0 to ubound(sWordList)
if bInnerQuotes then 'want to pass this as a single phrase
sReturnBuf = sReturnBuf & " " & sWordList(lCurPos)
if right(sWordList(lCurPos),1) = chr(34) then
sReturnBuf = left(sReturnBuf,len(sReturnBuf)-1) & "*" & chr(34)
sReturnBuf = sReturnBuf & " " & sANDorOR & " "'phrase is over
bInnerQuotes = False
end if
else
if left(sWordList(lCurPos),1) = chr(34) then
sReturnBuf = sReturnBuf & sWordList(lCurPos)
bInnerQuotes = True
else
sReturnBuf = sReturnBuf & chr(34) & sWordList(lCurPos) & "*" & _
chr(34) & " " & sANDorOR & " "'quote the word
end if
end if
next
'finally, remove the last AND or OR... and append the tick
formatContainsString = left(sReturnBuf,len(sReturnBuf)-(len(sANDorOR)+1)) _
& "'"
end function