Я использую функцию, созданную Крисом Моррисом и объясненную Дуэйном Кэмпсом в здесь .
Я изменил строку, чтобы проверить ложные срабатывания.
declare @list varchar(255) = 'Hello, Please Go to Phillip''s House who left long ago.';
create table table_1(WordID int, [Add] varchar(255));
insert into table_1(WordID, [Add]) values
(1, 'Hello'),
(2, 'Hi'),
(3, 'Go'),
(4, 'Come');
SELECT ( SELECT Item + ''
FROM dbo.PatternSplitCM(@list, '%[^a-zA-Z'']%') ps
WHERE NOT EXISTS( SELECT * FROM table_1 t WHERE ps.Item = t.[Add])
ORDER BY ItemNumber
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)')
Определение функции здесь:
-- PatternSplitCM will split a string based on a pattern of the form
-- supported by LIKE and PATINDEX
--
-- Created by: Chris Morris 12-Oct-2012
CREATE FUNCTION [dbo].[PatternSplitCM]
(
@List VARCHAR(8000) = NULL
,@Pattern VARCHAR(50)
) RETURNS TABLE WITH SCHEMABINDING
AS
RETURN
WITH numbers AS (
SELECT TOP(ISNULL(DATALENGTH(@List), 0))
n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) e (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) f (n),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) g (n))
SELECT
ItemNumber = ROW_NUMBER() OVER(ORDER BY MIN(n)),
Item = SUBSTRING(@List,MIN(n),1+MAX(n)-MIN(n)),
[Matched]
FROM (
SELECT n, y.[Matched], Grouper = n - ROW_NUMBER() OVER(ORDER BY y.[Matched],n)
FROM numbers
CROSS APPLY (
SELECT [Matched] = CASE WHEN SUBSTRING(@List,n,1) LIKE @Pattern THEN 1 ELSE 0 END
) y
) d
GROUP BY [Matched], Grouper;