Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert comma separated string into temporary table with individual rows
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
You can’t perform that action at this time.