-- ============================================= -- Author: iKwok -- Create date: 2013年3月21日 -- Description: 根据指定分隔符,把传入字符串分隔处理后,返回数据表, 函数功能类似.NET的string.Split() -- ============================================= CREATE FUNCTION [dbo].[f_Split](@text varchar(8000), @delimiter varchar(20) = ' ') RETURNS @tbResult TABLE ( id int IDENTITY PRIMARY KEY, value varchar(8000) ) AS BEGIN DECLARE @index int SET @index = -1 WHILE (LEN(@text) > 0) BEGIN SET @index = CHARINDEX(@delimiter , @text) IF (@index = 0) AND (LEN(@text) > 0) BEGIN INSERT INTO @tbResult VALUES (@text) BREAK END IF (@index > 1) BEGIN INSERT INTO @tbResult VALUES (LEFT(@text, @index - 1)) SET @text = RIGHT(@text, (LEN(@text) - @index)) END ELSE SET @text = RIGHT(@text, (LEN(@text) - @index)) END RETURN END GO