Split delimited string value into rows

前端 未结 3 1959
栀梦
栀梦 2020-12-10 09:25

Some external data vendor wants to give me a data field - pipe delimited string value, which I find quite difficult to deal with.

Without help from an application pr

3条回答
  •  情歌与酒
    2020-12-10 09:38

    Although your issue is probably long time resolved, I was looking for a solution to the very same problem you had. I solved it with the help of a procedure referenced here with slight adaptions to serve multi-byte characters (such as the German Umlauts) in the string by using CHAR_LENGTH() instead of LENGTH().

    DELIMITER $$
        CREATE FUNCTION SPLIT_STRING(val TEXT, delim VARCHAR(12), pos INT) RETURNS TEXT
        BEGIN
            DECLARE output TEXT;
            SET output = REPLACE(SUBSTRING(SUBSTRING_INDEX(val, delim, pos), CHAR_LENGTH(SUBSTRING_INDEX(val, delim, pos - 1)) + 1), delim, '');
            IF output = '' THEN
                SET output = null;
            END IF;
            RETURN output;
        END $$
    
        CREATE PROCEDURE TRANSFER_CELL()
        BEGIN
            DECLARE i INTEGER;
            SET i = 1;
            REPEAT
                INSERT INTO NewTuple (id, value)
                SELECT id, SPLIT_STRING(value, '|', i)
                FROM Tuple
                WHERE SPLIT_STRING(value, '|', i) IS NOT NULL;
                SET i = i + 1;
            UNTIL ROW_COUNT() = 0
            END REPEAT;
        END $$
    DELIMITER ;
    
    CALL TRANSFER_CELL() ;
    
    DROP FUNCTION SPLIT_STRING ;
    DROP PROCEDURE TRANSFER_CELL ;
    

提交回复
热议问题