2014-05-20 3 views
0

한 가지 문제가 있습니다.두 문자열 간의 단어 차이를 찾는 방법은 무엇입니까?

SQL Server 또는 타사 도구를 사용하여 단어를 두 문자열과 구별하고 싶습니다.

예 :

First String ==> "This is for test." 
Second String ==> "This is a for test." 

Output  ==> "a" from second string 

또는 :

First String ==> "abc This is for test." 
Second String ==> "This is a for test. This is for test." 

Output  ==> "abc" in first string and "a" from second string 
+0

시도 String.Replace를 사용하여. http://stackoverflow.com/questions/7517571/difference-between-two-strings-c-sharp – Nalaka526

+0

보기 : http://stackoverflow.com/questions/208094/how-to-find-difference-between -two-strings – Endrju

+0

첫 번째 문자열이 두 번째 문자열보다 길면 작동하지 않습니다. –

답변

4

는 SQL 서버는 가장 좋은 도구되지 않을 수도 있습니다,하지만 당신은이 같은 스크립트를, 모든 분할됩니다 사용할 수 있습니다 공백에있는 단어들과 목록 단어들 사이에 차이를 가져라. 그래서하지 출력에 포함됩니다 다른 텍스트에 존재하는 않는 한 텍스트의 단어는, 마지막으로 스크립트는 말을 concatinate됩니다

declare @str1 varchar(2000) = 'abc This is for test. dfg' 
declare @str2 varchar(2000) = 'This is a for test. This is for test.' 

declare @output1 varchar(2000) 
declare @output2 varchar(2000) 

SELECT @output1 = case when grp = 1 then coalesce(@output1+ ' ' + col, col) else @output1 end, 
     @output2 = case when grp = 2 then coalesce(@output2+ ' ' + col, col) else @output2 end 
FROM 
(values(@str1, @str2, 1),(@str2, @str1, 2)) x(str1, str2, grp) 
CROSS APPLY 
(
SELECT t.c.value('.', 'VARCHAR(2000)') col 
    FROM (
     SELECT x = CAST('<t>' + 
       REPLACE(str1, ' ', '</t><t>') + '</t>' AS XML) 
    ) a 
    CROSS APPLY x.nodes('/t') t(c) 
EXCEPT 
SELECT t.c.value('.', 'VARCHAR(2000)') 
    FROM (
     SELECT x = CAST('<t>' + 
       REPLACE(str2, ' ', '</t><t>') + '</t>' AS XML) 
    ) a 
    CROSS APPLY x.nodes('/t') t(c) 
) y 
SELECT @output1 FirstString, @output2 SecondString 

결과 :하는 방법을 구축

FirstString SecondString 
abc dfg  a 
+0

답장을 보내 주셔서 감사합니다. –

관련 문제