create function getstrofindex (@str varchar(8000),@index int =0)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next =1
select @location = charindex('#',@str,@start)
while (@location <>0 and @index > @next )
begin
select @start = @location +1
select @location = charindex('#',@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1
select @str_return = substring(@str,@start,@location -@start)
if (@index <> @next ) select @str_return = ''
return @str_return
end
go
select dbo.getstrofindex('aaa#bbb#1234#werwer#asdfsdf
',3)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next =1
select @location = charindex('#',@str,@start)
while (@location <>0 and @index > @next )
begin
select @start = @location +1
select @location = charindex('#',@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1
select @str_return = substring(@str,@start,@location -@start)
if (@index <> @next ) select @str_return = ''
return @str_return
end
go
select dbo.getstrofindex('aaa#bbb#1234#werwer#asdfsdf
',3)
declare @li_pos integer
select @ls_str = 'aaa#bbb#1234#werwer#asdfsdf'
select @li_pos = partindex('%#%',@ls_str)
while @li_pos > 0
begin
if @li_pos > 1
select @ls_temp = substring(@ls_str,1,@li_pos - 1)
print @ls_temp
end
select @ls_str = substring(@ls_str,@li_pos + 1,len(@ls_str))
select @li_pos = partindex('%#%',@ls_str)
end
if len(@ls_str) > 0
begin
select @ls_temp = @ls_str
print @ls_temp
end
--N要连继,
select top 8000 identity(int,1,1) as N into numtab from
(select top 100 id=1 from sysobjects) as a,
(select top 100 id=1 from sysobjects) as b,
(select top 100 id=1 from sysobjects) as c---------------------------------------------------------------------
declare @a table (id int,string varchar(8000))
insert @a select 1 ,'a,b,c,sd,dfsdfg'
union select 2, 'a,n,sdf,we,t'
union select 3, 's,df,df'select a.*,b.*,id,substring(','+string+',',N+1,charindex(',',','+string+',',N+1)-(N+1))
from @a a,numtab b
where substring(','+string+',',N,8000) like ',_%'
order by id,N