Created
November 27, 2014 12:36
-
-
Save Macagare/fd243befab58f48b366a to your computer and use it in GitHub Desktop.
convert comma separated string into temporary table with individual rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP procedure IF EXISTS split_string; | |
DELIMITER $$ | |
CREATE PROCEDURE split_string (list TEXT) | |
BEGIN | |
DECLARE max INT DEFAULT 0; | |
DECLARE i INT DEFAULT 1; | |
DECLARE elem INT DEFAULT 0; | |
SET max = length(list) - length(replace(list, ',', '')); | |
WHILE i < max DO | |
SET elem = SUBSTRING_INDEX(SUBSTRING_INDEX(list, ',', i), ',', -1); | |
INSERT IGNORE INTO table_destination (target_column) VALUES(elem); | |
SET i = i + 1; | |
END WHILE; | |
END;$$ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment