我有两个字符串列,如下所示
Column01 Column02
ABC School System Worked in Private school.
我希望匹配这些列,以便如果列A中的任何单个单词在列B中可用,则它会给出匹配结果。在这种情况下,“学校”一词出现在两列中,因此应将其视为匹配。
A栏中的单个单词可以定义为长度超过3的任何字母串,两侧有空格或者在一侧有任何空间而在另一侧没有空格(以迎合起点和结束语。)
我如何在SQL Server中执行此操作?
答案 0 :(得分:1)
您可以使用空格作为令牌创建XML
,然后使用.nodes()
获取xml
列中的字词并将它们进行比较。像这样的东西
WITH Table1 as
(
SELECT 1 as ID,'ABC School System' as col1, 'Worked in Private school' as col2
), CTE2 as
(
SELECT M.id,c.value('text()[1]','VARCHAR(100)') COLLATE Latin1_General_CI_AS as col1val,c2.value('text()[1]','VARCHAR(100)') COLLATE Latin1_General_CI_AS as col2val
FROM
(
SELECT id,
CONVERT(XML,'<i>' + REPLACE(col1,' ','</i><i>') + '</i>') col1,
CONVERT(XML,'<i>' + REPLACE(col2,' ','</i><i>') + '</i>') col2
FROM Table1
)M
CROSS APPLY col1.nodes('i') as t(c)
CROSS APPLY col2.nodes('i') as t2(c2)
)
SELECT ID,col1val,col2val
FROM CTE2
WHERE col1val = col2val
AND LEN(col1val) > 3
AND LEN(col2val) > 3