Question How to Get the words in Phrase and put in SQL command

tresehulyo

Member
Joined
Sep 1, 2009
Messages
16
Programming Experience
Beginner
for example the input phrase is "Help me Please"

then the sql command would be

SELECT * FROM table Where Column='Help' OR Column='me' OR Column='Please'
 
To get the words in the phrase you can use the String.Split() method:
VB.NET:
       Dim selectParams As String() = inputPhrase.Split(New [Char]() {" "c})

Look up String.Split() for more on that.

If you are using SqlServer (or most other Sql DBMS) then strictly speaking you should use parameterized queries so something like:

VB.NET:
  Dim selectBuilder As New StringBuilder("SELECT * FROM table Where ")

  for index As Integer = 0 to selectParams.Length - 1
    selectBuilder.Append("Column=@Param" & index.ToString
    if index < selectParams.Length - 1 Then
      selectBuilder.Append(" OR ")
    End If
  Next

Then to use the SELECT statement simply selectBuilder.ToString. The selectParams array contains the words to use.

If you insist on doing it as in your post :) then modify the above to:
VB.NET:
  Dim selectBuilder As New StringBuilder("SELECT * FROM table Where ")

  for index As Integer = 0 to selectParams.Length - 1
    selectBuilder.Append("Column='" & selectParams(index) & "'")
    if index < selectParams.Length - 1 Then
      selectBuilder.Append(" OR ")
    End If
  Next

Then as above selectBuilder.ToString will give you the SELECT statement.

Hope this helps. :)
 

Latest posts

Back
Top