The reprint link cannot be found, please let me know if you have any trouble.
The following is the reproduced content:
--Support multi-byte separator --How to use --select * from dbo.split('abc-def-ghi-jkl','-') --select * from dbo.split('abc----def----ghi----jkl','----') CREATE FUNCTION [dbo].[split](@Long_str varchar(max),@split_str varchar(100)) RETURNS @tmp TABLE( ID int IDENTITY PRIMARY KEY, short_str varchar(max) ) AS BEGIN DECLARE @long_str_Tmp varchar(max), @short_str varchar(max), @split_str_length int SET @split_str_length = LEN(@split_str) IF CHARINDEX(@split_str,@Long_str)=1 SET @long_str_Tmp=SUBSTRING(@Long_str, @split_str_length+1, LEN(@Long_str)-@split_str_length) ELSE SET @long_str_Tmp=@Long_str IF CHARINDEX(REVERSE(@split_str),REVERSE(@long_str_Tmp))>1 SET @long_str_Tmp=@long_str_Tmp+@split_str ELSE SET @long_str_Tmp=@long_str_Tmp WHILE CHARINDEX(@split_str,@long_str_Tmp)>0 BEGIN SET @short_str=SUBSTRING(@long_str_Tmp,1, CHARINDEX(@split_str,@long_str_Tmp)-1) DECLARE @long_str_Tmp_LEN INT,@split_str_Position_END int SET @long_str_Tmp_LEN = LEN(@long_str_Tmp) SET @split_str_Position_END = LEN(@short_str)+@split_str_length SET @long_str_Tmp=REVERSE(SUBSTRING(REVERSE(@long_str_Tmp),1, @long_str_Tmp_LEN-@split_str_Position_END)) IF @short_str<>'' INSERT INTO @tmp SELECT @short_str END RETURN END
The above is the first split method.
Reproduce a similar split method:
The original text is reproduced from: http://www.cnblogs.com/zc_0101/archive/2009/06/30/1513776.html
The reproduced content is as follows:
--SQL Server Split function --Author:zc_0101 --illustrate: --Support multi-byte separator --Instructions --Select * FROM DBO.F_SQLSERVER_SPLIT('1203401230105045','0') --select * from DBO.F_SQLSERVER_SPLIT('abc1234a12348991234','1234') --Select * from DBO.F_SQLSERVER_SPLIT('ABC',',') CREATE FUNCTION F_SQLSERVER_SPLIT(@Long_str NVARCHAR(MAX),@split_str NVARCHAR(100)) RETURNS @tmp TABLE( ID inT IDENTITY PRIMARY KEY, short_str NVARCHAR(MAX) ) AS BEGIN DECLARE @short_str NVARCHAR(MAX),@split_str_length int,@split_str_Position_Begin int SET @split_str_length = LEN(@split_str) SET @Long_str=REPLACE(REPLACE(@Long_str,CHAR(10),''),CHAR(13),'') IF CHARINDEX(@split_str,@Long_str)=1 SET @Long_str=STUFF(@Long_str,1,@split_str_length,'') IF CHARINDEX(@split_str,@Long_str)=0 INSERT INTO @tmp SELECT @Long_str ELSE BEGIN WHILE 1>0 BEGIN SET @split_str_Position_Begin = CHARINDEX(@split_str,@Long_str) SET @short_str=LEFT(@Long_str,@split_str_Position_Begin-1) IF @short_str<>'' INSERT INTO @tmp SELECT @short_str SET @Long_str=STUFF(@Long_str,1,@split_str_Position_Begin+@split_str_length-1,'') SET @split_str_Position_Begin = CHARINDEX(@split_str,@Long_str) IF @split_str_Position_Begin=0 BEGIN IF LTRIM(@Long_str)<>'' INSERT INTO @tmp SELECT @Long_str BREAK END END END RETURN END
The above is the second split method.
Finally, reprint a summary:
The original text is reproduced from: http://www.cnblogs.com/aierong/archive/2008/11/19/sqlserver_split.html
The following is the reproduced content:
--Method 0: Dynamic SQL method 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 --Method 1: Loop interception method CREATE FUNCTION f_splitSTR( @s varchar(8000), -- string to be split @split varchar(10) -- data separator )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 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 --Method 2: Use the temporary split auxiliary table method CREATE FUNCTION f_splitSTR( @s varchar(8000), -- string to be split @split varchar(10) -- data separator )RETURNS @re TABLE(col varchar(100)) AS BEGIN --Create an auxiliary table for split processing (only table variables can be manipulated in user-defined functions) DECLARE @t TABLE(ID int IDENTITY,b bit) 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 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 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tb_splitSTR]') and objectproperty(id,N'IsUserTable')=1) drop table [dbo].[tb_splitSTR] GO --Method 3: Use permanent split auxiliary table method -- string splitting auxiliary table SELECT TOP 8000 ID=IDENTITY(int,1,1) INTO dbo.tb_splitSTR FROM syscolumns a,syscolumns b GO --String split processing function CREATE FUNCTION f_splitSTR( @s varchar(8000), -- string to be split @split varchar(10) -- data separator )RETURNS TABLE AS 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: Use OUTER APPLY of sql server2005 --Remarks: It must be run under 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 )