方式一:經過XQuery(須要SQL Server 2005以上版本)。 node
create function func_splitid oracle培訓機構 git
(@str varchar(max),@split varchar(10)) oracle
RETURNS @t Table (c1 int) spa
AS .net
BEGIN 字符串
DECLARE @x XML get
SET @x = CONVERT(XML,'') string
INSERT INTO @t SELECT x.item.value('@id[1]', 'INT') FROM @x.nodes('//items/item') AS x(item) it
RETURN io
END
執行:select * from dbo.func_splitid('1,2,3,4,5,6', ',')
結果:
方式二:經過charindex和substring。
create function func_splitstring
(@str nvarchar(max),@split varchar(10))
returns @t Table (c1 varchar(100))
as
begin
declare @i int
declare @s int
set @i=1
set @s=1
while(@i>0)
begin
set @i=charindex(@split ,@str ,@s)
if(@i>0)
begin
insert @t(c1) values(substring(@str ,@s ,@i-@s))
end
else begin
insert @t(c1) values(substring(@str ,@s ,len(@str)-@s+1))
end
end
return
end
執行:select * from dbo.func_splitstring('1,2,3,4,5,6', ',')
結果: