SQL 按照特定字符分割字符串

新建表值函数:

 

USE  TestDb
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[f_split](@c VARCHAR(2000),   @split VARCHAR(2)) 
 returns @t TABLE(col VARCHAR(20)) 
 AS 
   BEGIN 
       WHILE( Charindex(@split, @c) <> 0 ) 
         BEGIN 
             INSERT @t(col) 
             VALUES (Substring(@c, 1, Charindex(@split, @c) - 1)) 
 
             SET @c = Stuff(@c, 1, Charindex(@split, @c), '') 
         END  
       INSERT @t (col) VALUES (@c)  
       RETURN 
   END


 

你可能感兴趣的:(SQL,Server)