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

前端 未结 4 403
清酒与你
清酒与你 2020-12-22 07:32

I have a record in a single column.Like below

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


        
相关标签:
4条回答
  • 2020-12-22 07:35

    Try This:

        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"
    
    0 讨论(0)
  • 2020-12-22 07:41

    Here's a function that will split a string inline....

    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 : Chennai

    Here's a table valued function that will return a table of values

    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 
    
    0 讨论(0)
  • 2020-12-22 07:43

    Try this if you are using 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 
    
    0 讨论(0)
  • 2020-12-22 07:58

    i wrote a function for this, long time ago, nothing fancy, but sufficient:

    -- =============================================
    -- 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
    
    0 讨论(0)
提交回复
热议问题