我需要一个人的帮助。我有两个字符串。
First String ==> "This is for test."
Second String ==> "This is a for test."
我希望输出基于以下条件。
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输入
First String ==> "This is for test."
Second String ==> "This is a for test."
然后输出
TRUE
IF输入
First String ==> "This is for test."
Second String ==> "This is a "
然后输出
TRUE
IF输入
First String ==> "This is for test."
Second String ==> "This "
然后输出
FALSE
答案 0 :(得分:0)
在Sql Server 2008中有一个可用于此任务的SplitBy函数:
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
创建此功能后,您可以轻松地完成以下任务:
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
我希望它有所帮助!