20050519 修订为不使用 PARSENAME ,鸣谢楼下的"小杰"网友: !@#$%^&*!
且支持分隔符支持任意长度!
ALTER function dbo.UDF_SplitSubString
(
@Expression varchar(8000)
,@Delimiter varchar(100)
,@ int
)
returns varchar(8000)
as
begin
--declare @Expression varchar(8000)
--set @Expression = 'abc@@pppp@aa'
--declare @Delimiter varchar(100)
--set @Delimiter = '@@@'
--declare @ int
--set @ = 3
declare @p int
set @p = CharIndex(@Delimiter,@Expression)
if @p > 0
begin
set @p = @p + len(@Delimiter) - 1
end
declare @i int
set @i = 1
while @i < @
begin
set @i = @i + 1
set @Expression = substring (@Expression, @p + 1,len(@Expression) - @p )
set @p = CharIndex(@Delimiter,@Expression)
if @p > 0
begin
set @p = @p + len(@Delimiter) - 1
end
else
begin
break
end
end
declare @s varchar(1000)
if @p = 0 and @i = @
begin
set @s = @Expression
end
else if @i = @
begin
set @s = substring(@Expression, 1,@p - len(@Delimiter))
end
return @s
end
GO
declare @ varchar(1000)
set @ = '@@playyuer@www&&.Microshaoft&&com@90.90@@pppp@.'
declare @d varchar(1000)
set @d = '@@'
select
dbo.UDF_SplitSubString (@ , @d, 0)
,dbo.UDF_SplitSubString (@ , @d, 1)
,dbo.UDF_SplitSubString (@ , @d, 2)
,dbo.UDF_SplitSubString (@ , @d, 3)
,dbo.UDF_SplitSubString (@ , @d, 4)
,dbo.UDF_SplitSubString (@ , @d, 5)
,dbo.UDF_SplitSubString (@ , @d, 6)
,dbo.UDF_SplitSubString (@ , @d, 7)
,dbo.UDF_SplitSubString (@ , @d, 8)
,dbo.UDF_SplitSubString (@ , @d, 100)
(
@Expression varchar(8000)
,@Delimiter varchar(100)
,@ int
)
returns varchar(8000)
as
begin
--declare @Expression varchar(8000)
--set @Expression = 'abc@@pppp@aa'
--declare @Delimiter varchar(100)
--set @Delimiter = '@@@'
--declare @ int
--set @ = 3
declare @p int
set @p = CharIndex(@Delimiter,@Expression)
if @p > 0
begin
set @p = @p + len(@Delimiter) - 1
end
declare @i int
set @i = 1
while @i < @
begin
set @i = @i + 1
set @Expression = substring (@Expression, @p + 1,len(@Expression) - @p )
set @p = CharIndex(@Delimiter,@Expression)
if @p > 0
begin
set @p = @p + len(@Delimiter) - 1
end
else
begin
break
end
end
declare @s varchar(1000)
if @p = 0 and @i = @
begin
set @s = @Expression
end
else if @i = @
begin
set @s = substring(@Expression, 1,@p - len(@Delimiter))
end
return @s
end
GO
declare @ varchar(1000)
set @ = '@@playyuer@www&&.Microshaoft&&com@90.90@@pppp@.'
declare @d varchar(1000)
set @d = '@@'
select
dbo.UDF_SplitSubString (@ , @d, 0)
,dbo.UDF_SplitSubString (@ , @d, 1)
,dbo.UDF_SplitSubString (@ , @d, 2)
,dbo.UDF_SplitSubString (@ , @d, 3)
,dbo.UDF_SplitSubString (@ , @d, 4)
,dbo.UDF_SplitSubString (@ , @d, 5)
,dbo.UDF_SplitSubString (@ , @d, 6)
,dbo.UDF_SplitSubString (@ , @d, 7)
,dbo.UDF_SplitSubString (@ , @d, 8)
,dbo.UDF_SplitSubString (@ , @d, 100)
使用 T-SQL: PARSENAME 函数,参阅了:
T-SQL 拆分 IP 地址
http://www.cnblogs.com/montaque/archive/2004/11/24/67909.html
鸣谢原作者 montaque : !@#$%^&*
alter function dbo.UDF_SplitSubString
(
@Expression varchar(8000)
,@Delimiter char(1)
,@ int
,@Dot char(1) = '$'
)
returns varchar(200)
as
begin
declare @p int
set @p = CharIndex(@Delimiter,@Expression)
declare @i int
set @i = 1
while @i < @
begin
set @Expression = substring (@Expression, @p + 1,len(@Expression) - @p )
set @p = CharIndex (@Delimiter,@Expression)
set @i = @i + 1
end
declare @s varchar(1000)
if @p = 0
begin
set @s = @Expression
end
else
begin
--替换一下 '.' 因为 parsename 只认 '.'
set @Expression = replace(@Expression,'.',@Dot)
set @Expression = substring(@Expression,1,@p - 1) + '.' + substring(@Expression, @p + 1,len(@Expression) - @p)
set @s = ParseName(@Expression,2)
set @s = replace(@s,@Dot,'.')
end
return @s
end
go
--测试
declare @ varchar(1000)
set @ = '@playyuer@www.Microshaoft.com@90.90@@pppp@aa'
select
dbo.UDF_SplitSubString (@ , '@', 0,default)
,dbo.UDF_SplitSubString (@ , '@', 1,default)
,dbo.UDF_SplitSubString (@ , '@', 2,default)
,dbo.UDF_SplitSubString (@ , '@', 3,default)
,dbo.UDF_SplitSubString (@ , '@', 4,default)
,dbo.UDF_SplitSubString (@ , '@', 5,default)
,dbo.UDF_SplitSubString (@ , '@', 6,default)
,dbo.UDF_SplitSubString (@ , '@', 7,default)
,dbo.UDF_SplitSubString (@ , '@', 8,default)
,dbo.UDF_SplitSubString (@ , '@', 100,default)
(
@Expression varchar(8000)
,@Delimiter char(1)
,@ int
,@Dot char(1) = '$'
)
returns varchar(200)
as
begin
declare @p int
set @p = CharIndex(@Delimiter,@Expression)
declare @i int
set @i = 1
while @i < @
begin
set @Expression = substring (@Expression, @p + 1,len(@Expression) - @p )
set @p = CharIndex (@Delimiter,@Expression)
set @i = @i + 1
end
declare @s varchar(1000)
if @p = 0
begin
set @s = @Expression
end
else
begin
--替换一下 '.' 因为 parsename 只认 '.'
set @Expression = replace(@Expression,'.',@Dot)
set @Expression = substring(@Expression,1,@p - 1) + '.' + substring(@Expression, @p + 1,len(@Expression) - @p)
set @s = ParseName(@Expression,2)
set @s = replace(@s,@Dot,'.')
end
return @s
end
go
--测试
declare @ varchar(1000)
set @ = '@playyuer@www.Microshaoft.com@90.90@@pppp@aa'
select
dbo.UDF_SplitSubString (@ , '@', 0,default)
,dbo.UDF_SplitSubString (@ , '@', 1,default)
,dbo.UDF_SplitSubString (@ , '@', 2,default)
,dbo.UDF_SplitSubString (@ , '@', 3,default)
,dbo.UDF_SplitSubString (@ , '@', 4,default)
,dbo.UDF_SplitSubString (@ , '@', 5,default)
,dbo.UDF_SplitSubString (@ , '@', 6,default)
,dbo.UDF_SplitSubString (@ , '@', 7,default)
,dbo.UDF_SplitSubString (@ , '@', 8,default)
,dbo.UDF_SplitSubString (@ , '@', 100,default)
顺便说一句:
Ms SQL 2K SP4 is avaialable!
http://www.microsoft.com/downloads/details.aspx?FamilyId=8E2DFC8D-C20E-4446-99A9-B7F0213F8BC5&displaylang=zh-cn
http://download.microsoft.com/download/9/b/f/9bff6646-2cdb-4069-ada0-548be9cb9338/ReadmeSql2k32sp4.htm#_additional_information_about_sp4