I have a table variable which contains a list of words that I want to replace from query results.
I want to search for any of these 600
words in the table variable. I did 3 just for this example. Then, I want to replace the word if it's found. I'm getting results back but it's duplicating per word. I have a UDF which takes each word in the company name and looks to see if it's a match.
declare @findreservedwords table
(findWord varchar(50) primary key)
INSERT INTO @findreservedwords
VALUES
('Inc','LLC','Corp.')
--actually I have over 500 records in the @findreservedwords table variable. Just 3 for this example
select distinct p.product_id,replace(c.Company_Name,f.findword,'') as NewCompanyName,f.findWord,sep.col
FROM PRODUCT p
INNER JOIN COMPANY c on p.Manufacturer_ID = c.company_id
CROSS APPLY dbo.SeparateValues(c.company_name, ' ') sep
LEFT OUTER JOIN @findreservedwords f on f.findWord = sep.col
WHERE p.product_id = 100
This Returns...
Product_ID NewCompanyName FindWord Col
100 null null Sony
100 Sony Inc LLC LLC
100 Sony LLC Inc Inc
I would like to have it return just one result and both the "LLC" and the "Inc" would be removed since those words are in the reserved words table variable. So the string, "Sony LLC Inc"
would be...
Product_ID NewCompanyName
100 Sony