Skip to content

Instantly share code, notes, and snippets.

@wchiquito
Last active December 25, 2015 18:39
Show Gist options
  • Select an option

  • Save wchiquito/7021866 to your computer and use it in GitHub Desktop.

Select an option

Save wchiquito/7021866 to your computer and use it in GitHub Desktop.
Split a string using common_schema and insert the tokens in a table.
/* Need to install common_schema - code.google.com/p/common-schema/ */
/* Procedure structure for procedure `explode1` */
/*!50003 DROP PROCEDURE IF EXISTS `explode1` */;
DELIMITER $$
/*!50003 CREATE PROCEDURE `explode1`(str varchar(65500), delim VARCHAR(255))
BEGIN
DECLARE _iteration, _num_tokens INT UNSIGNED DEFAULT 0;
DROP TEMPORARY TABLE IF EXISTS `temp_explode`;
CREATE TEMPORARY TABLE `temp_explode` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `word` VARCHAR(200), PRIMARY KEY (`id`));
SET _num_tokens := (SELECT `common_schema`.`get_num_tokens`(str, delim));
WHILE _iteration < _num_tokens DO
SET _iteration := _iteration + 1;
INSERT INTO `temp_explode` (`word`) SELECT `common_schema`.`split_token`(str, delim, _iteration);
END WHILE;
SELECT `id`, `word` FROM `temp_explode`;
DROP TEMPORARY TABLE IF EXISTS `temp_explode`;
END */$$
DELIMITER ;
/* TEST */
CALL `explode1`('Lorem Ipsum is simply dummy text of the printing and typesetting', CHAR(32));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment