1参数定义和说明
输出参数:@splitString varchar(8000) -- 需要Split的字符串
@separate varchar(10) ---分隔符
返回:@returnTable table(col_Value varchar(20)) --单个字符长度可以按照需要修改
2,函数体
CREATE FUNCTION SplitStr (@splitString varchar(8000), @separate varchar(10))
RETURNS @returnTable table(col_Value varchar(20))
AS
BEGIN
declare @thisSplitStr varchar(20)
declare @thisSepIndex int
declare @lastSepIndex int
set @lastSepIndex = 0
if Right(@splitString ,len(@separate)) <> @separate set @splitString = @splitString @separate
set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex)
while @lastSepIndex <= @thisSepIndex
begin
set @thisSplitStr = SubString(@splitString ,@lastSepIndex,@thisSepIndex-@lastSepIndex)
set @lastSepIndex = @thisSepIndex 1
set @thisSepIndex = CharIndex(@separate,@splitString ,@lastSepIndex)
insert into @returnTable values(@thisSplitStr)
end
return
END
下面是使用的例子:
使用实例:select * from SplitStr(‘123,456,789’,’,’)
结果:
col_value
-----------------------------------------------------------------------------
123
456
789