--方法0:动态SQL法
declare @s varchar(100),@sql varchar(1000)
set @s='1,2,3,4,5,6,7,8,9,10'
set @sql='select col='''+ replace(@s,',',''' union all select ''')+''''
PRINT @sql
exec (@sql)
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_splitSTR]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_splitSTR]
GO
--方法1:循环截取法
CREATE FUNCTION f_splitSTR(
@s varchar(8000), --待分拆的字符串
@split varchar(10) --数据分隔符
)RETURNS @re TABLE(col varchar(100))
AS
BEGIN
DECLARE @splitlen int
SET @splitlen=LEN(@split+'a')-2
WHILE CHARINDEX(@split,@s)>0
BEGIN
INSERT @re VALUES(LEFT(@s,CHARINDEX(@split,@s)-1))
SET @s=STUFF(@s,1,CHARINDEX(@split,@s)+@splitlen,'')
END
INSERT @re VALUES(@s)
RETURN
END
GO
EXISTS IF (SELECT * from) dbo.sysobjects WHERE ID = object_id (N '[the dbo]. [f_splitSTR]' and in xtype (N'FN ', N'IF', N'TF '))
drop function [the dbo] . [f_splitSTR]
the GO
- method 2: method using temporary auxiliary table split
the CREATE f_splitSTR the FUNCTION (
@s VARCHAR (8000), - a string to be split
@split varchar (10) - the data delimiter
) RETURNS tABLE @re (COL VARCHAR (100))
the AS
the BEGIN
- split auxiliary table creating processing (user-defined functions in the operation table only variable)
the DECLARE @t tABLE (ID int the IDENTITY, B 'bit)
the INSERT @t (B) SELECT TOP 8000 0 FROM syscolumns a, syscolumns b
INSERT @re SELECT SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID)
FROM @t
WHERE ID<=LEN(@s+'a')
AND CHARINDEX(@split,@split+@s,ID)=ID
RETURN
END
GO
EXISTS IF (SELECT * from) dbo.sysobjects WHERE ID = object_id (N '[the dbo]. [f_splitSTR]' and in xtype (N'FN ', N'IF', N'TF '))
drop function [the dbo] . [f_splitSTR]
the GO
IF EXISTS (SELECT * WHERE from dbo.sysobjects ID = object_id (N '[the dbo]. [tb_splitSTR]') and OBJECTPROPERTY (ID, N'IsUserTable ') =. 1)
drop Table [the dbo]. [ tb_splitSTR]
the GO
- method 3: method using an auxiliary table permanent split
- split auxiliary string table
the SELECT ID = 8000 the TOP the IDENTITY (int, 1,1) the INTO dbo.tb_splitSTR
the FROM the syscolumns A, B the syscolumns
the GO
- string handler split
the CREATE f_splitSTR the fUNCTION (
@s VARCHAR (8000), - a string to be split
@split varchar (10) - the data separator
) the RETURNS TABLE
the AS
the RETURN (
SELECT col=CAST(SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID) as varchar(100))
FROM tb_splitSTR
WHERE ID<=LEN(@s+'a')
AND CHARINDEX(@split,@split+@s,ID)=ID)
GO
- Method 4: Using the OUTER APPLY sql server2005
CREATE FUNCTION [dbo].[ufn_SplitStringToTable]
(
@str VARCHAR(MAX) ,
@split VARCHAR(10)
)
RETURNS TABLE
AS
RETURN
( SELECT B.id
FROM ( SELECT [value] = CONVERT(XML , '<v>' + REPLACE(@str , @split , '</v><v>')
+ '</v>')
) A
OUTER APPLY ( SELECT id = N.v.value('.' , 'varchar(100)')
FROM A.[value].nodes('/v') N ( v )
) B
)
instruction manual:
Method 4 must only be run in sql server2005
Annotation: The above article is cited in http://www.cnblogs.com/aierong/archive/2008/11/19/sqlserver_split.html
Reproduced in: https: //www.cnblogs.com/-maomao/p/4495183.html