---可接受自定义的单个字符作为分隔符
---返回表值
ALTER FUNCTION [dbo].[fnSplit](@Expression NVARCHAR(200),@Delimiter NVARCHAR(1),@Start_location INT)
-- @Experssion 包含子字符串和分隔符的表达式
-- @Delimiter 用于标示子字符串界限的任何字符
-- @Start_location 在@experssion中开始搜索的位置
RETURNS @retTable TABLE (Col NVARCHAR(10))
AS
BEGIN
IF @Start_location<=0
SET @start_location=1
DECLARE @ExpressionLen INT, ---子字符串长度
@Delimiter_location INT --标识符的出现位置
SET @Expression=@Expression+@Delimiter
SET @ExpressionLen=LEN(@Expression)
WHILE @Start_location<=@ExpressionLen
BEGIN
SET @Delimiter_location=CHARINDEX(@Delimiter,@Expression,@Start_location)
IF @Delimiter_location>@Start_location
INSERT INTO @retTable VAlUES(SUBSTRING(@Expression,@Start_location,@Delimiter_location-@Start_location))
SET @Start_Location=@Delimiter_location+1
END
RETURN
END
例:SELECT * FROM dbo.split('abc,ab,c',',',1)
结果:
abc
ab
a
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15042150/viewspace-594428/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/15042150/viewspace-594428/