Baz
Baz

Reputation: 13123

Match sentences containing a given word

I have a sentence column in my table. I wish to select all sentences which contain a given word.

Words contain only the following letters: a-z, áéíóú

The only other character is a single space, separating each word in the sentence. There are no spaces at the start or end of a sentence. So sentences look like this:

"i am here"
"no im here"

selecting sentences containing the word "i" should only match the first sentence above.

How should I select these rows from my table?

Upvotes: 1

Views: 646

Answers (1)

John Sheridan
John Sheridan

Reputation: 446

Based upon the information you have provided there are three permutations you need to cover.

  • The word you are searching for is the first word in the sentence. In this case there will be no space before the word but there will be a space after the word.
  • The word you are searching for is not the first word nor the last word in the sentence. In this case there will be a space either side of the word.
  • The word you are searching for is the last word in the sentence. In this case there will be a space before the word but none after the word.

So your query would look something like this ...

SELECT * FROM YourTableName 
    WHERE SentenceCol LIKE 'YOUR-SEARCH-WORD %' 
    OR SentenceCol LIKE '% YOUR-SEARCH-WORD %' 
    OR SentenceCol LIKE '% YOUR-SEARCH-WORD'

Upvotes: 2

Related Questions