CREATE FUNCTIONDbo. SPLIT (@Long_str NVARCHAR(MAX),@split_str NVARCHAR( -)) RETURNS @tmp TABLE(IDInT IDENTITY PRIMARY KEY, Short_strNVARCHAR(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(Ten),"'),CHAR( -),"') 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 ENDGO
SELECT from Dbo. SPLIT ('A-st', ',')
SQL Server SPLIT