0

I need one favor. I have two string.

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

I want output based on below condition.

If first string half count of words or all words exist in second string then it will return true.
If first string more then half count words is not exist in second string then it will return false.

OP

IF Input

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

then Output

 TRUE

IF Input

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

then Output

 TRUE

IF Input

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

then Output

 FALSE
AB Vyas
  • 2,349
  • 6
  • 26
  • 43
  • similar to your last question... http://stackoverflow.com/questions/23753152/how-to-differentiate-two-string-using-sql-server-or-any-third-party – Tanner May 20 '14 at 09:54
  • Impossible to tell given your "requirements". I'd parse each string into tokens and calculate some sort of match percentage. true/false would be decided based a threshold. I'd look into something like Python's NLTK. Something like this might also help: https://en.wikipedia.org/wiki/String_metric – duffymo May 20 '14 at 09:55

1 Answers1

0

There is a SplitBy Function you can use for this task in Sql Server 2008:

create FUNCTION [dbo].[SplitBy](@String varchar(8000), @Delimiter char(1))
returns @temptable
TABLE (nameIndex int identity(1,1),items varchar(8000))
as
begin
    declare @idx int
    declare
    @slice varchar(8000)

    select @idx = 1
    if len(@String)<1 or @String
    is null return

    while @idx!= 0
    begin
    set @idx =
    charindex(@Delimiter,@String)
    if @idx!=0
    set @slice = left(@String,@idx
    - 1)
    else
    set @slice = @String

    if(len(@slice)>0)
    insert
    into @temptable(Items) values(@slice)

    set @String =
    right(@String,len(@String) - @idx)
    if len(@String) = 0 break
    end

return
end

After creating this function, you can easily handle this task like:

declare @string nvarchar(max), @string2 nvarchar(max)
set @string='This is for test'; 
set @string2='This'; 

SELECT * into #temp from [SplitBy](@string,' ') 
SELECT * into #temp2 from [SplitBy](@string2,' ') 

declare @countWordsFirstString int = (select COUNT(*) from #temp)

declare @countWordsResult int = (
   select COUNT(*) 
   from #temp t
   join #temp2 t2 on t.items=t2.items
)

print (case when @countWordsResult >= @countWordsFirstString/2 then 'True' else 'False' end)

drop table #temp
drop table #temp2

I hope it helps!

Reference for SplitBy Function: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e676a8e3-ae55-44b6-b36c-fdcd1ef3c70b/sql-to-split-the-string-based-on-the-space-example-split-the-firstname-alone-from-the-complete?forum=transactsql

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82