if OBJECT_ID('fn_split') is not null drop function fn_split go -- ============================================= -- 标题:字符串分隔 -- ============================================= create function fn_split ( @Value varchar(2000), -- 需要分隔的字符串 @Separator varchar(20) -- 分隔符 ) returns @ReturnTable table(col varchar(100)) as begin while(charindex(@Separator,@Value) <> 0) begin insert @ReturnTable(col) values (substring(@Value, 1, charindex(@Separator, @Value) - 1)) set @Value = stuff(@Value, 1, charindex(@Separator, @Value), '') end insert @ReturnTable(col) values (@Value) return end go -- 测试 select * from dbo.fn_split('1,2,3', ',')