DROP PROCEDURE IF EXISTS SPLIT_STRING;
DELIMITER //
CREATE PROCEDURE SPLIT_STRING (
IN fullstr VARCHAR(1024),
IN delim VARCHAR(1)
)
SQL SECURITY INVOKER
BEGIN
DECLARE inipos INTEGER;
DECLARE endpos INTEGER;
DECLARE maxlen INTEGER;
DECLARE item VARCHAR(1024);
SET inipos = 1;
SET fullstr = CONCAT(fullstr, delim);
SET maxlen = LENGTH(fullstr);
REPEAT
SET endpos = LOCATE(delim, fullstr, inipos);
SET item = SUBSTR(fullstr, inipos, endpos - inipos);
IF item <> '' AND item IS NOT NULL THEN
SELECT item;
END IF;
SET inipos = endpos + 1;
UNTIL inipos >= maxlen END REPEAT;
END//
DELIMITER ;
SET @agg = "G1;G2;G3;G4;" ;
call SPLIT_STRING(@agg, ';');