简体   繁体   中英

Split a column into multiple columns in a SQL query using pipe delimiter

I have below data in a column of a table, I want to split it into further columns. | is used as the separator in this scenario . Column header should be before : & after column is its value.

Column
-----------------------------------------------------------------------------
ID: 30000300 | Name: India | Use: New Use
ID: 30000400 | Name: Aus | New ID: 15625616 | Address 1: NEW Rd
ID: 30000400 | Name: USA | City: VIA ARAMAC | New ID: 123
ID: 30000500 | Name: Russia | New ID: 15624951 | Address 2: 2131 BEAUDESERT

Output should be:

ID          Name    Use New ID  City     Address 1   Address 2   New City
----------------------------------------------------------------------
30000300    India   New Use                 
30000400    Aus         15625616        NEW Rd      
30000400    USA         15625616    VIA ARAMAC          GALILEE 
30000500    Russia      15624951            2131 BEAUDESERT

You have several rows that contain key value pairs inside an nvarchar column, but you want a table that has a header based on the keys and then rows containing just the values, sans keys. There is first the issue of an input like Key1: Value1 | Key2: Value2 . Should this be returned as

Key1 Key2
Value1 NULL
NULL Value2

or is this not a possible scenario? Either way, there is the issue of generating a table with dynamic column names.

The problem with your question is that this is not a scenario that would normally be solved via SQL. You should get the data in your programming language of choice, then use regular expressions or split methods to get what you need.

If you insist doing it via SQL, then the solution is to turn the original lines input into another string, that you then sp_executesql ( https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql ), but I do NOT recommend it.

Here is a partial answer that you can use to return the n-th entry in a delimited string:

DECLARE @DelimitedString VARCHAR(8000);
DECLARE @Delimiter VARCHAR(100);
DECLARE @indexToReturn INT;



DECLARE @tblArray TABLE
(
    ElementID INT IDENTITY(1, 1), -- Array index
    Element VARCHAR(1000)         -- Array element contents
);
-- Local Variable Declarations
-- ---------------------------
DECLARE @Index SMALLINT,
        @Start SMALLINT,
        @DelSize SMALLINT;

SET @DelSize = LEN(@Delimiter + 'x') - 1;



-- Loop through source string and add elements to destination table array
-- ----------------------------------------------------------------------
WHILE LEN(@DelimitedString) > 0
BEGIN

    SET @Index = CHARINDEX(@Delimiter, @DelimitedString);

    IF @Index = 0
    BEGIN

        INSERT INTO @tblArray
        (
            Element
        )
        VALUES
        (LTRIM(RTRIM(@DelimitedString)));

        BREAK;
    END;
    ELSE
    BEGIN

        INSERT INTO @tblArray
        (
            Element
        )
        VALUES
        (LTRIM(RTRIM(SUBSTRING(@DelimitedString, 1, @Index - 1))));

        SET @Start = @Index + @DelSize;
        SET @DelimitedString = SUBSTRING(@DelimitedString, @Start, LEN(@DelimitedString) - @Start + 1);

    END;
END;

DECLARE @val VARCHAR(1000);
SELECT @val = Element
FROM @tblArray AS ta
WHERE ta.ElementID = @indexToReturn;

SELECT @val;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM