2

I would like to do something like this i.e., use wild card characters in the in clause:

SELECT * FROM mytable WHERE keywords IN ('%test%', '%testing%')

This is not supported in SQL Server.... Is there some other way to achieve it...

Looking for something other than:

SELECT * FROM mytable WHERE keywords like '%test%' or keywords like '%testing%' or.....
gbn
  • 422,506
  • 82
  • 585
  • 676
StackUnderflow
  • 24,080
  • 14
  • 54
  • 77
  • I presume your actual usage is more complex, but in the example you gave you only need LIKE '%test%' since anything that matches '%testing%' also matches '%test%'. Also, if you have control over your database structure, you'd be better off normalizing the storage of keywords. – Larry Lustig Mar 30 '10 at 21:40
  • @larry lustig: I was just giving a toy example... I want to do something little complex.. thanks – StackUnderflow Mar 30 '10 at 21:54

4 Answers4

8

Nope, only other way I can think of is joining in to a temp table but then you have to eliminate duplicate rows.

This query of yours is horribly inefficient as it will unconditionally table scan. Perhaps you should look at separating the keywords or introducing a full text index.

If you would like to look into doing full text searches, then I would recommend looking into some of the heavy weights out there like sphinx or lucene when you evaluate the Sql Server full-text solution.

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
3
SELECT DISTINCT *
FROM
  mytable M
  JOIN
  (
  SELECT '%test%' AS pattern
  UNION ALL SELECT '%testing%'
  ...
  ) foo ON M.keywords LIKE foo.Pattern

Could be a CTE or temp table too

gbn
  • 422,506
  • 82
  • 585
  • 676
2

This is not supported in MS SQL.... Is there some other way to achieve it?

Yes, Full Text Search. At least for prefix wildcards.

Community
  • 1
  • 1
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
0

You can use Regular Expression to do this. In one Regular Expression you can define as many pattern as you want in one expression.

There are lot's of article about Regular Expression Matching in MS SQL SERVER. I used RLIKE in MySQL to do Regular Expression Matching.

I also attached link1 & link2 for MS SQL SERVER Regular Expression Matching.

Imrul
  • 3,456
  • 5
  • 32
  • 27