I have a table with the columns: word, category, description, countWord.
I need to check for each and every word in column word, how many times it’s exists in the description column for every category and set the countWord column:
example:
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
DROP TABLE #TestData;
CREATE TABLE #TestData (
WORD VARCHAR(50) NOT NULL,
Description VARCHAR(1000) NOT NULL,
countWord INT DEFAULT (0)
);
INSERT #TestData (WORD, Description)
VALUES ('Acrylics', 'This is something about Acrylics and Acrylics accessories.');
UPDATE td SET
td.countWord = (x.DescLen - x.RepLen) / x.WordLen
FROM
#TestData td
CROSS APPLY ( VALUES (
LEN(td.WORD),
LEN(td.Description),
LEN(REPLACE(td.Description, td.WORD, ''))
) ) x (WordLen, DescLen, RepLen);
SELECT
*
FROM
#TestData td;