sql2008里自定义SPLIT分隔字符串函数

if object_id ( ' dbo.fn_split ' ) is not null drop function dbo.fn_split
go
create function dbo.fn_split
(
@inputstr varchar ( 8000 ),
@seprator varchar ( 10 )
)
returns @temp table (a varchar ( 200 ))
as
begin
declare @i int
set @inputstr = rtrim ( ltrim ( @inputstr ))
set @i = charindex ( @seprator , @inputstr )
while @i >= 1
begin
insert @temp values ( left ( @inputstr , @i - 1 ))
set @inputstr = substring ( @inputstr , @i + 1 , len ( @inputstr ) - @i )
set @i = charindex ( @seprator , @inputstr )
end
if @inputstr <> ' / '
insert @temp values ( @inputstr )
return
end
go
select * from dbo.fn_split( ' 10_20_30 ' , ' _ ' )

你可能感兴趣的:(split)