SQL Server 2008 R2 - 如何拆分我的 varchar 列字符串并获取第三个索引字符串

SQL Server 2008 R2 - How to split my varchar column string and get 3rd index string

我在单column.Like下面有记录

Address
Arulraj   | Guindy   | Chennai | TamilNaadu | India | 600042 |  | 10000001 
Adaiakalm | Chenanai | Chennai | TamilNaadu | India | 600042 |  | 10000001 

如何分别得到分割后的字符串"TamilNaadu"、"Chennai "、"India "

我很久以前为此写了一个函数,没什么特别的,但足够了:

-- =============================================
-- Author:      Nico Boey
-- Create date: 
-- Description: Returns the n'th part of a given string, split by a given separator, with n being a zero based index
-- =============================================
ALTER FUNCTION [dbo].[GetSplitStringPart] 
(
    @StringToSplit nvarchar(255)
    , @Separator nvarchar(10)
    , @Index int
)
RETURNS nvarchar(255)
AS
BEGIN
    -- Declare the return variable here
    DECLARE 
        @Result nvarchar(255)
        , @SepPos int
        , @PrevSepPos int
        , @currentIndex int
    if @Separator is null
        select @Result = @StringToSplit
    else if @StringToSplit is not null and @Index > -1
    begin
        -- init
        select @currentIndex = 0, @PrevSepPos = 0
        -- read ahead
        select @SepPos = charindex(@Separator , @StringToSplit)
        if @SepPos = 0 select @SepPos = len(@StringToSplit) + 1
        -- loop until index is reached
        while @currentIndex <= @Index
        begin
            if @currentIndex = @Index 
            begin
                select @Result = substring(@StringToSplit, @PrevSepPos+1, @SepPos-@PrevSepPos-1)
            end
            select @currentIndex = @currentIndex + 1
            select @PrevSepPos = @SepPos
            if @PrevSepPos = len(@StringToSplit) + 1 break;
            select @SepPos = charindex(@Separator , @StringToSplit, @PrevSepPos + 1)
            if @SepPos = 0 select @SepPos = len(@StringToSplit) + 1
        end
    end
    -- Return the result of the function
    RETURN @Result
END

这是一个将内联字符串拆分的函数....

CREATE FUNCTION [dbo].[FN_SPLIT] ( --SELECT DBO.FN_SPLIT('TEST1 , TEST2', 2, ',')
    @s varchar(512),
    @i int,
    @sep char(1) = ',')
RETURNS varchar(512)
AS
BEGIN
    DECLARE @Ret    VARCHAR(512);

    WITH Pieces(pn, start, stop) AS (
      SELECT 1, 1, CHARINDEX(@sep, @s)
      UNION ALL
      SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
      FROM Pieces
      WHERE stop > 0
    )
    SELECT @Ret =
    RTRIM(SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END))
    FROM Pieces
    WHERE pn = @i

    RETURN @Ret;
END

USAGE : SELECT DBO.FN_SPLIT('Address Arulraj | Guindy| Chennai | TamilNaadu | India | 600042 | | 10000001 ', 3, '|')

RETURNS : 钦奈

这是一个 table 值函数,它将 return table 个值

CREATE FUNCTION [dbo].[FN_SPLIT_TBL](@InExp varchar(8000), @Sep varchar(10)) --SELECT * FROM DBO.[FN_SPLIT_TBL]('TEST1,TEST2', ',')
RETURNS @Res    TABLE(
    Pos         int,
    Value       varchar(max))
AS
BEGIN
    WITH Pieces(pn, start, stop) AS (
        SELECT 1, 1, CHARINDEX(@Sep, @InExp)
        UNION ALL
        SELECT pn + 1, stop + 1, CHARINDEX(@sep, @InExp, stop + 1)
        FROM Pieces
        WHERE stop > 0
    )

    INSERT INTO @Res
    SELECT pn, SUBSTRING(@InExp, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
    FROM Pieces OPTION (MAXRECURSION 0);

    RETURN;
END

USAGE : SELECT *
FROM DBO.FN_SPLIT_TBL('Address Arulraj | Guindy| Chennai | TamilNaadu | India | 600042 | | 10000001 ', '|')
--WHERE Pos = 3 -- Uncomment this to only return index 3

RETURNS :

Pos Value
1   Address Arulraj 
2    Guindy
3    Chennai 
4    TamilNaadu 
5    India 
6    600042 
7    
8    10000001 

如果您正在使用 Sql Server 2012+

,请试试这个
DECLARE @str   VARCHAR(1000)='Adaiakalm | Chenanai | Chennai | TamilNaadu | India | 600042 |  | 10000001',
        @sql   NVARCHAR(max),
        @index INT=3

SET @str = '''' + Replace(@str, '|', ''',''') + ''''
SET @sql= 'select choose('+ CONVERT(VARCHAR(10), @index) + ', ' + @str + ')'

EXEC Sp_executesql @sql 

试试这个:

    DECLARE @str   VARCHAR(1000)='Adaiakalm | Chenanai | Chennai | TamilNaadu | India | 600042 |  | 10000001',
        @part2 INT , @part3 INT , @part4 INT , @part5 INT ,@part6   INT = 1
set @part3 = CHARINDEX('|',@str,CHARINDEX('|',@str)+1)
set @part4 = CHARINDEX('|',@str,CHARINDEX('|',@str,CHARINDEX('|',@str)+1)+1)
set @part5 = CHARINDEX('|',@str,CHARINDEX('|',@str,CHARINDEX('|',@str,CHARINDEX('|',@str)+1)+1)+1)
set @part6 = CHARINDEX('|',@str,CHARINDEX('|',@str,CHARINDEX('|',@str,CHARINDEX('|',@str,CHARINDEX('|',@str)+1)+1)+1)+1)
select SUBSTRING(@str,@part3+1,@part4 - @part3-1) As "Part-3",SUBSTRING(@str,@part4+1,@part5 - @part4-1) As "Part-4",SUBSTRING(@str,@part5+1,@part6 - @part5-1) As "Part-5"