类型:转载 责任编辑:asp 日期:2007/03/01
有一字符串: aaa#bbb#1234#werwer#asdfsdf
现在要在sqlserver 7中根据一个变量i得到x的值如下:
i=1
x=aaa
i=2
x=bbb
i=3
x=1234
......
求解?
推荐阅读
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)