sql2008里自定义SPLIT分隔字符串函数
ifobject_id('dbo.fn_split')isnotnulldropfunction dbo.fn_split
go
createfunction dbo.fn_split
(
@inputstrvarchar(8000),
@sepratorvarchar(10)
)
returns@temptable (a varchar(200))
as
begin
declare@iint
set@inputstr=rtrim(ltrim(@inputstr))
set@i=charindex(@seprator, @inputstr)
while@i>=1
begin
insert@tempvalues(left(@inputstr, @i-1))
set@inputstr=substring(@inputstr, @i+1, len(@inputstr) -@i)
set@i=charindex(@seprator, @inputstr)
end
if@inputstr<>'/'
insert@tempvalues(@inputstr)
return
end
go
select*from dbo.fn_split('10_20_30','_')
go
createfunction dbo.fn_split
(
@inputstrvarchar(8000),
@sepratorvarchar(10)
)
returns@temptable (a varchar(200))
as
begin
declare@iint
set@inputstr=rtrim(ltrim(@inputstr))
set@i=charindex(@seprator, @inputstr)
while@i>=1
begin
insert@tempvalues(left(@inputstr, @i-1))
set@inputstr=substring(@inputstr, @i+1, len(@inputstr) -@i)
set@i=charindex(@seprator, @inputstr)
end
if@inputstr<>'/'
insert@tempvalues(@inputstr)
return
end
go
select*from dbo.fn_split('10_20_30','_')