CREATE FUNCTION [dbo].[GetSplitOfIndex]
(
@String NVARCHAR(MAX) , --要分割的字符串
@split NVARCHAR(10) , --分隔符号
@index INT --取第几个元素
)
RETURNS NVARCHAR(1024)
AS
BEGIN
DECLARE @location INT
DECLARE @start INT
DECLARE @next INT
DECLARE @seed INT
SET @String = LTRIM(RTRIM(@String))
SET @start = 1
SET @next = 1
SET @seed = LEN(@split)
SET @location = CHARINDEX(@split, @String)
WHILE @location <> 0
AND @index > @next
BEGIN
SET @start = @location + @seed
SET @location = CHARINDEX(@split, @String, @start)
SET @next = @next + 1
END
IF @location = 0
SELECT @location = LEN(@String) + 1
RETURN SUBSTRING(@String,@start,@location-@start)
END