本文主要是介绍Sql Server字符串拆分(Split)方法汇总,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
--方法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)--方法1:循环截取法
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]
GOCREATE FUNCTION f_splitSTR(
@s varchar(8000), --待分拆的字符串
@split varchar(10) --数据分隔符
)RETURNS @re TABLE(col varchar(100))
AS
BEGINDECLARE @splitlen intSET @splitlen=LEN(@split+'a')-2WHILE CHARINDEX(@split,@s)>0BEGININSERT @re VALUES(LEFT(@s,CHARINDEX(@split,@s)-1))SET @s=STUFF(@s,1,CHARINDEX(@split,@s)+@splitlen,'')ENDINSERT @re VALUES(@s)RETURN
END
GO--方法2:使用临时性分拆辅助表法
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]
GOCREATE FUNCTION f_splitSTR(
@s varchar(8000), --待分拆的字符串
@split varchar(10) --数据分隔符
)RETURNS @re TABLE(col varchar(100))
AS
BEGIN--创建分拆处理的辅助表(用户定义函数中只能操作表变量)DECLARE @t TABLE(ID int IDENTITY,b bit)INSERT @t(b) SELECT TOP 8000 0 FROM syscolumns a,syscolumns bINSERT @re SELECT SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID)FROM @tWHERE ID<=LEN(@s+'a') AND CHARINDEX(@split,@split+@s,ID)=IDRETURN
END
GO--方法3:使用永久性分拆辅助表法
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--字符串分拆辅助表
SELECT TOP 8000 ID=IDENTITY(int,1,1) INTO dbo.tb_splitSTR
FROM syscolumns a,syscolumns b
GO
--字符串分拆处理函数
CREATE FUNCTION f_splitSTR(
@s varchar(8000), --待分拆的字符串
@split varchar(10) --数据分隔符
)RETURNS TABLE
AS
RETURN(SELECT col=CAST(SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID) as varchar(100))FROM tb_splitSTRWHERE ID<=LEN(@s+'a') AND CHARINDEX(@split,@split+@s,ID)=ID)
GO--方法4:循环字符串分割
create FUNCTION [dbo].[Fun_SplitStr]
(@originalStr VARCHAR(8000), --要分割的字符串@split varchar(100) --分隔符号
)
RETURNS @temp TABLE(Result VARCHAR(100))
AS
BEGINDECLARE @result AS VARCHAR(100); --定义变量用于接收单个结果 SET @originalStr = @originalStr + @split ; WHILE (@originalStr <> '')BEGINSET @result = LEFT(@originalStr, CHARINDEX(@split, @originalStr, 1) -1) ; INSERT @temp VALUES(@result) ; --STUFF()函数用于删除指定长度的字符,并可以在指定的起点处插入另一组字符。SET @originalStr = STUFF(@originalStr, 1, CHARINDEX(@split, @originalStr, 1), '');END RETURNEND--方法5:利用sql server2005的OUTER APPLYCREATE FUNCTION [dbo].[ufn_SplitStringToTable]
(@str VARCHAR(MAX) ,@split VARCHAR(10)
)
RETURNS TABLEAS
RETURN( SELECT B.idFROM ( SELECT [value] = CONVERT(XML , '<v>' + REPLACE(@str , @split , '</v><v>')+ '</v>')) AOUTER APPLY ( SELECT id = N.v.value('.' , 'varchar(100)')FROM A.[value].nodes('/v') N ( v )) B)
这篇关于Sql Server字符串拆分(Split)方法汇总的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!