Sqlserver string interception function

sqlserver string interception function

create function


Create FUNCTION [dbo].[f_split_to_table]
       (
         @string VARCHAR(MAX) ,
         @separator CHAR
       )
RETURNS @return TABLE ( value VARCHAR(200) )
AS
BEGIN
    DECLARE @len INT;
    SET @len = LEN(@string);
    IF ( SUBSTRING(@string, @len, 1) <> @separator )
       SET @string = @string + @separator; 
    DECLARE @strtemp VARCHAR(200);
    DECLARE @index1 INT;
    DECLARE @index2 INT;
    SET @index1 = 0;
    SET @index2 = CHARINDEX(@separator, @string, 1);

    WHILE @index2 <> 0
          BEGIN
                SET @strtemp = SUBSTRING(@string, @index1 + 1,
                                         @index2 - @index1 - 1);
                INSERT  @return
                        ( [value] )
                VALUES  ( @strtemp );
                SET @index1 = @index2;
                SET @index2 = CHARINDEX(@separator, @string, @index1 + 1);
          END;
    RETURN;
END;

call method

dbo.f_split_to_table( @goodsids,',')

Guess you like

Origin blog.csdn.net/qq_42455262/article/details/129894950