SQLSERVER常用功能——字符串分割

	CREATE FUNCTION [dbo].[f_split]

	

	(

	

	  @SourceSql VARCHAR(8000) ,

	

	  @StrSeprate VARCHAR(10)

	

	)

	

	RETURNS @temp TABLE ( RowId INT ,COL VARCHAR(100) )

	

	AS

	

	    BEGIN

	

	        DECLARE @i INT,@j INT=1

	

	        SET @SourceSql = RTRIM(LTRIM(@SourceSql))

	

	        SET @i = CHARINDEX(@StrSeprate, @SourceSql)

	

	        WHILE @i >= 1

	

	            BEGIN

	    

	                INSERT  @temp

	

	                VALUES  (@j, LEFT(@SourceSql, @i - 1) )

	

	                SET @SourceSql = SUBSTRING(@SourceSql, @i + 1,

	

	                                           LEN(@SourceSql) - @i)

	

	                SET @i = CHARINDEX(@StrSeprate, @SourceSql)

	    SET @j=@j+1

	            END

	

	        IF @SourceSql <> '\'

	

	            INSERT  @temp

	

	            VALUES  ( @j,@SourceSql )

	

	        RETURN

	

	    END

 

你可能感兴趣的:(sqlserver)