简体   繁体   中英

How to find word differences between two strings?

I am stuck in one problem.

I want to differentiate words from two strings using SQL Server or Any third party tool.

eg:

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

Output        ==>   "a" from second string 

or:

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 

Sql-server may not be the best tool, but you could use a script like this, it will split up all the words at the spaces and take the difference between the list words. So a word in 1 text that doesn't exists in the other text will be included in the output, finally the script will concatinate the words:

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

Result:

FirstString  SecondString
abc dfg      a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM