![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER FUNCTION [dbo].[f_Split](@temp varchar(8000),@parting char(1))
--f_Split
--@temp为参数名 varchar为参数类型 8000为类型长度 @parting为参数名 char类型
--分割SQL字符串
RETURNS varchar(8000)
--返回类型为varchar长度为4000
AS
BEGIN
Declare @i int,@str char(15),@formattemp char(12),@s varchar(8000)
--SET @temp='0000022016,0000022022,0000022017,0000022021'
set @s=''
SET @temp=Rtrim(Ltrim(@temp))
SET @i=charIndex(@parting,@temp)--从temp起始位置开始搜索"."的起始位置
--create table #t (ip char(15))--创建临时表
While @i>=1
BEGIN
set @str=left(@temp,@i-1)
--insert into #t values(@str)
set @s=@s+''''+rtrim(ltrim(@str))+''''+','
SET @temp=substring(@temp,@i+1,len(@temp)-@i)
SET @i=charIndex(@parting,@temp)
END
--set @s=@s+''''+rtrim(ltrim(@str))+''''+','
set @s=@s+''''+@temp+''''
return Substring(@s,1,len(@s))
END
print dbo.f_split('0000022016,0000022022,0000022017,0000022021',',')