I have a small problem and I know a few solutions for it, but I don’t know the best way (or less dirty spaghetti way) to go.
I have a string variable, and I need to use it in a like expression.
So:
declare @a varchar(100) = 'my string to use as a join from'
select *
from table
where
column like '%' + @a + '%'
But I don’t want any rows from table that contains the @a variable, I want any rows from table that are contained in the @a variable, so:
select *
from table
where
@a like '%' + column + '%'
Result:
'my string'
'as a join from'
But now I need to remove the matched rows from the @a variable. How can I do that?
(edit)
expected result:
@a = ' to use '
'my string'
'as a join from'
You can alter the value of
@awith each matched row in the select:Demo: http://www.sqlfiddle.com/#!3/187d6/5 (result:
@a = ' to use ')If you want to get a result set as well, use a temp table to store matching rows:
Demo: http://www.sqlfiddle.com/#!3/187d6/13