2

Working in MS SQL 2005 and I want to use a select statement within a wildcard where clause like so:

SELECT text
FROM table_1
WHERE ID LIKE '%SELECT ID FROM table_2%'

I'm looking for product ids within a large body of text that is held in a DB. The SELECT statement in the wildcard clause will return 50+ rows. The statement above is obviously not the way to go. Any suggestions?

Spencer Carnage
  • 2,056
  • 2
  • 28
  • 41
  • HAve you tried using an IN clause instead of a LIKE? – websch01ar Jun 18 '10 at 20:24
  • I tried the IN clause which didn't work. Then again, I'm not be writing the statement correctly. The SELECT statement in the wildcard clause is going to return 50+ results which might change everyone's answer a bit. – Spencer Carnage Jun 18 '10 at 20:49

1 Answers1

2

You can do a join and construct the like string based on table_2.

SELECT * FROM table_1 t1
INNER JOIN table_2 t2 ON t1.ID LIKE '%' + CONVERT(VARCHAR, t2.ID) + '%'
Ken Richards
  • 2,937
  • 2
  • 20
  • 22