Split function of SQLserver

There is no built-in split function in SQLserver, so you need to build a table valued function yourself. The specific code is as follows:

 ----------the following method is to use","to split strings 
CREATE FUNCTION [dbo].[Split](@separator VARCHAR(64)=',',@string NVARCHAR(max))
RETURNS @ResultTab TABLE (
Id    INT ,
Res     NVARCHAR(500) 
)
AS
BEGIN
DECLARE @Num INT

IF(@string IS NOT NULL AND @string <> '' AND LEN(@string)>0)
BEGIN
   IF(CHARINDEX(@separator,@string)> 0) -- determine if the character to be truncated exists 
   BEGIN
       SET @Num=0
       WHILE (CHARINDEX(@separator,@string)> 0) -- if the character to be truncated exists, continue looping 
       BEGIN
           SET @Num=@Num+1
           
           INSERT INTO @ResultTab(Id,Res )-- extract strings and insert table variables 
           SELECT @Num,LEFT(@string,CHARINDEX(@separator,@string)-1)
           
 --delete the truncated and inserted string 
           SET @string=STUFF(@string,1,CHARINDEX(@separator,@string)-1+LEN(@separator),'')    
       END
       
 --if the last truncated string is empty, then it will not be inserted 
 --for example :'0123,0456,0789,'the last remaining string of this kind is an empty string 
       IF(@string IS NOT NULL AND @string <> '')
       BEGIN
           INSERT INTO @ResultTab(Id,Res)
           SELECT @Num+1,@string 
      END            
   END
   ELSE
   BEGIN
       INSERT INTO @ResultTab(Id,Res) SELECT 1,@string 
   END
END
ELSE
BEGIN
   DELETE FROM @ResultTab
END
RETURN
END

For example, using "," to split strings" 001002003004"

SQL statement: select * from [Split] (',',' 001002003004');

Result: