日期:2014-05-16 浏览次数:20437 次
--创建函数
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
--调用
select GetSplitOfIndex(col , ',' , 2) from table group by GetSplitOfIndex(col , ',' , 2)