0

I have a table like this:

  ID     Sentence
  1      A fox jumped out of the car, and ran away. 
  2      An Elephant was seen in the jungle. 
  3      A fox jumped out of the car, and ran towards jungle. 
  4      A dog was inside the car, and ran towards jungle. 

I need to search for Words like Car, ran, and jungle.These words should be presented in the given sentences.

My output should be the following rows, since only those sentences have all those key words:

  ID     Sentence  
  3      A fox jumped out of the car, and ran towards jungle. 
  4      A dog was inside the car, and ran towards jungle. 

I tried doing this in SQL and it gives me the output which I want but I wanted to know, if there is any better way to do this.

 Create table #temp1(ID int, Sentence varchar(1000)) 

 insert into #temp1 values
  (1,      'A fox jumped out of the car, and ran away.') 
 ,(2,      'An Elephant was seen in the jungle.')
 ,(3,      'A fox jumped out of the car, and ran towards jungle.')
 ,(4,      'A dog was inside the car, and ran towards jungle.')


 select * from #temp1 
 where Sentence like '%car%' and Sentence like '%jungle%' and Sentence like '%ran%'      

I get the same output which I wanted.

 ID Sentence

 3  A fox jumped out of the car, and ran towards jungle.
 4  A dog was inside the car, and ran towards jungle.
Avi
  • 1,795
  • 3
  • 16
  • 29
  • 1
    You shoudl look at https://learn.microsoft.com/en-us/sql/t-sql/statements/create-fulltext-index-transact-sql?view=sql-server-2017 – lukaszberwid Oct 14 '18 at 19:16
  • Possible duplicate of [Is there a combination of "LIKE" and "IN" in SQL?](https://stackoverflow.com/questions/3014940/is-there-a-combination-of-like-and-in-in-sql) – ahmed abdelqader Oct 14 '18 at 19:35

0 Answers0