The error I got running the snippet was:
Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '='.
The below code isn't an ideal solution - it will only return a list of companies where their name has been changed by the REPLACE function.
declare @companies table (id int, name nvarchar(50))
INSERT INTO @companies(id, name)
SELECT 1,'One Company' UNION ALL
SELECT 2, 'Two co.' UNION ALL
SELECT 3, 'Three incorporated' UNION ALL
SELECT 4, 'Four inc.' UNION ALL
SELECT 5, 'Five llc' UNION ALL
SELECT 6, 'Six llp' UNION ALL
SELECT 7, 'Seven ltd'
select * from @companies
declare @badStrings table (item varchar(50))
INSERT INTO @badStrings(item)
SELECT 'company' UNION ALL
SELECT 'co.' UNION ALL
SELECT 'incorporated' UNION ALL
SELECT 'inc.' UNION ALL
SELECT 'llc' UNION ALL
SELECT 'llp' UNION ALL
SELECT 'ltd'
select * from @badStrings
Here is the edited query:
select id, x.Name
from @companies c
OUTER APPLY (
SELECT Replace(c.name, item, '') AS [Name]
FROM @badStrings
) x
where c.name != ''
AND x.[Name] != c.Name
This returns:
id Name
----------- --------
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
7 Seven
(7 row(s) affected)
Hopefully it's useful
Edit:
An alternative to apply the match to those company names which end with the @badStrings value
select id, x.Name
from @companies c
OUTER APPLY (
SELECT Replace(c.name, item, '') AS [Name]
FROM @badStrings
WHERE c.Name LIKE '%'+item
) x
where c.name != ''