1. 程式人生 > >SQL 按照特定字元分割字串

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