create function f_splitstr(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(F1 varchar(100))
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>''
insert @temp values(@SourceSql)
return
end
-执行
select * from dbo.f_splitstr('asdfcv,qe.ehxa.what about this key i;qe, adsf.dfawgf','.')
where f1 like '%key%'F1
----------------------------------------------------------------------------------------------------
what about this key i;qe, adsf(所影响的行数为 1 行)
create function f_find(
@str varchar(8000), --被搜索的字符串
@key varchar(1000) --搜索的关键字
)
returns varchar(8000)
as
begin
declare @i int,@len int,@l varchar(8000),@r varchar(8000)
set @i=charindex(@key,@str)
if @i>0
begin
select @len=len(@key)
,@l=left(@str,@i-1)
,@l=substring(@l,charindex('.',ltrim(reverse(@l)))+1,8000)
,@r=substring(@str,@i,8000)
,@r=left(@r,charindex('.',@r)-1)
,@l=@l+@r
end
return(@l)
end
goselect dbo.f_find('......asdfcv,qe.ehxa.what about this key i;qe, adsf.dfawgf......'
,'key')
go
drop function f_find
create function f_find(
@str varchar(8000), --被搜索的字符串
@key varchar(1000) --搜索的关键字
)
returns varchar(8000)
as
begin
declare @i int,@len int,@l varchar(8000),@r varchar(8000)
set @i=charindex(@key,@str)
if @i>0
begin
select @len=len(@key)
,@l=left(@str,@i-1)
,@r=substring(@str,@i,8000)
,@l=substring(@l,charindex('.',ltrim(reverse(@l)))+1,8000)
,@i=charindex('.',@r)
,@r=case @i when 0 then @r else left(@r,@i-1) end
,@l=@l+@r
end
return(@l)
end
goselect dbo.f_find('aakeyaa','key')
go
drop function f_find
应为
@l=substring(@l,charindex('.',len(@l)-ltrim(reverse(@l)))+3,8000)
col
-------------------------
What about you, Sir.
WOW, you.
Tell me what did you saw.
代码如下:
declare @str varchar(8000), @st varchar(8000), @key varchar(50)
declare @tab table(col varchar(8000))
set @str='Today is xxx. What about you, Sir. Here is my book. It''s the best show I''ve ever seen. WOW, you. Oh, my God. Tell me what did you saw.'
set @key='you'
set @st=@str
--set @tab=''
declare @i int, @len int, @a varchar(8000), @b varchar(8000), @c varchar(8000), @d varchar(8000), @e varchar(8000), @l varchar(8000)
,@f int
set @f=0
while (1=1)
begin
if (charindex(@key, @st)<=0) break
set @i=charindex (@key, @st)
if @i>0
begin
select @len=len(@key)
,@a=left(@st, @i-1)
,@c=substring (@st, @i, 8000)
,@b=substring (@a, len(@a)-charindex ('.', ltrim (reverse (@a)))+3, 8000)
,@e=charindex ('.', @c)
,@d=case @i when 0 then @c else left (@c, @e) end
,@l=@b + @d
insert @tab values (@l)
set @f=charindex(@l, @st)+len(@l)
set @st=substring(@st, @f, len(@st)-@f+1)
end
end
select * from @tab
现用两个字符串调试
@a = 'Today is xxx. What about you, Sir. Here is my book. It''s the best show I''ve ever seen. WOW, you. Oh, my God. Tell me what did you saw.'
@b = 'you know. i know you. he know you.'
(@b内,每句均有关键字出现;@a内,关键字偶尔出现几次)
用你的方法,@a搜索不齐全;用我的方法,@b搜索不齐全现小弟做如下更改,便可通过上述两参数的搜索select @len=len(@key)
,@a=left(@st, @i-1)
,@c=substring (@st, @i, 8000)
,@b=substring (@a, charindex ('.', rtrim(@a))+1, 8000)
,@e=charindex ('.', @c)
,@d=case @i when 0 then @c else left (@c, @e) end
,@l=@b + @dinsert @tab values (@l)
set @f=charindex(@l, @st)+len(@l)
set @st=substring(@st, @f+1, len(@st)-@f+1)