慕标5832272
SQL Server Split函数--Author:zc_0101--说明:--支持分割符多字节--使用方法--Select * FROM DBO.F_SQLSERVER_SPLIT('1203401230105045','0')--select * from DBO.F_SQLSERVER_SPLIT('abc1234a12348991234','1234')--Select * from DBO.F_SQLSERVER_SPLIT('ABC',',')1 CREATE FUNCTION F_SQLSERVER_SPLIT(@Long_str NVARCHAR(MAX),@split_str NVARCHAR(100))2 RETURNS @tmp TABLE(3 ID inT IDENTITY PRIMARY KEY,4 short_str NVARCHAR(MAX)5 )6 AS7 BEGIN8 DECLARE @short_str NVARCHAR(MAX),@split_str_length int,@split_str_Position_Begin int9 SET @split_str_length = LEN(@split_str)10 SET @Long_str=REPLACE(REPLACE(@Long_str,CHAR(10),''),CHAR(13),'')11 IF CHARINDEX(@split_str,@Long_str)=112 SET @Long_str=STUFF(@Long_str,1,@split_str_length,'')13 IF CHARINDEX(@split_str,@Long_str)=014 INSERT INTO @tmp SELECT @Long_str15 ELSE16 BEGIN17 WHILE 1>018 BEGIN19 SET @split_str_Position_Begin = CHARINDEX(@split_str,@Long_str)20 SET @short_str=LEFT(@Long_str,@split_str_Position_Begin-1)21 IF @short_str<>'' INSERT INTO @tmp SELECT @short_str22 SET @Long_str=STUFF(@Long_str,1,@split_str_Position_Begin+@split_str_length-1,'')23 SET @split_str_Position_Begin = CHARINDEX(@split_str,@Long_str)24 IF @split_str_Position_Begin=025 BEGIN26 IF LTRIM(@Long_str)<>''27 INSERT INTO @tmp SELECT @Long_str28 BREAK29 END30 END31 END32 RETURN33 END