1.fun_split split string to form a return table
CREATEFUNCTION [dbo].[Fun_split]( @Items VARCHAR(MAX), @SplitStr VARCHAR(MAX))RETURNS @SplitTable TABLE(ItemVARCHAR(MAX)) asBEGIN DECLARE @Split_Index INT=0; DECLARE @Split_len INT=0; SET @Items = RTRIM(LTRIM(@Items)); SET @Split_Index = CHARINDEX(@SplitStr,@Items); SET @Split_len=LEN(@SplitStr); while(@Split_Index>=1) BEGIN INSERT into @SplitTable VALUES( Left(@Items,@Split_Index-1)); SET @Items = SUBSTRING(@Items,@Split_Index + @Split_len,LEN(@Items)-@Split_Index); SET @Split_Index = CHARINDEX(@SplitStr,@Items); END IF(@Items<>"') INSERT into @SplitTable VALUES(@Items); RETURNEND
2.fun_arrlen split string, return length
CREATE FUNCTION [dbo].[Fun_arrlen]( @Str varchar(Max), @SplitStr varchar(Max))RETURNS int asbegin Declare @i Int Set @i =(Len(@Str)- Len(Replace(@Str,@SplitStr,"')))/Len(@SplitStr)+1 Return(@i)End
3.fun_getstrindex analog Array Get value
CREATE FUNCTION [dbo].[Fun_getstrindex]( @Str varchar(Max), @SplitStr varchar(Max), @index int)RETURNS varchar(Max) asbegin Declare @location int Declare @start int Declare @next int Declare @seed int Set @str=LTrim(RTrim(@str)) Set @start=0 Set @next=0 Set @seed=Len(@SplitStr) Set @location=charindex(@SplitStr,@str) while @location<>0 and @index>@next begin Set @start=@location+@seed Set @location=charindex(@SplitStr,@str,@start) Set @next=@next+1 End if @location =0 Select @location =Len(@str)+1 return substring(@str,@start,@location-@start);End
Operation Result:
SQL Server Functions