問題:
給定字串『7003442784,7003333444,8003014272,7003059011,7003305105,7003059206,7003305105,8003014272,7003442784,7003305105』,查詢裡面重複出現過的字串
預期結果:
7003442784,8003014272,7003305105
經過努力,在sql中實現了該效果。
將sql語句貼到這裡,
declare @string nvarchar(max)
declare @newstring nvarchar(max)
declare @spacemark nvarchar(1)
declare @sreachstring nvarchar(max)
declare @id bigint
set @id=0
set @spacemark=','
set @sreachstring=''
set @string='7003442784,7003333444,8003014272,7003059011,7003305105,7003059206,7003305105,8003014272,7003442784,7003305105'
while(len(@string)>0)
begin
if charindex(@spacemark,@string)>1
begin
set @newstring=substring(@string,1,charindex(@spacemark,@string)-1)
set @string=substring(@string,charindex(@spacemark,@string)+1,len(@string)-charindex(@spacemark,@string))
if charindex(@newstring,@string)>0
begin
set @sreachstring +=@newstring+','
set @string=replace(@string,@newstring,'')
set @string=replace(@string,',,',',')
endend
else
if charindex(@spacemark,@string)=1
begin
if len(@string)>1
begin
set @string=substring(@string,2,len(@string)-1)
endelse
begin
set @string=''
endend
endif len(@sreachstring)>0
begin
set @sreachstring=substring(@sreachstring,1,len(@sreachstring)-1)
endselect @sreachstring
執行後得到預期結果:
7003442784,8003014272,7003305105
求字串中重複出現的最長字串
求字串中重複出現的最長字串 例如字串 drgabcifrabcsdrrs中,最長公共字串是 abc 方法 利用字尾樹來求。字串的字尾樹有如下 drgabcifrabcsdrrs rgabcifrabcsdrrs gabcifrabcsdrrs abcifrabcsdrrs s1 rabcsdrrs ...
字串中重複出現的最長字串 字尾樹思想
字串中重複出現的最長字串maxlenrepeatsubstring.cpp shanghai zilaishui laizi haishanghai output shanghai author arhaiyun date 2013 09 23 include stdafx.h include in...
查詢字串
在乙個主串中查詢相應的子串,如 abcdwoshidef 中查詢 woshi 方法 該實現的方法是最簡單的模式匹配方法,時間複雜度較高 include iostream using namespace std int searchstring const char str1,const char s...