kedar.nitty-witty.com
Thursday September 9th 2010

MySQL Stored procedure – Split Delimited string into Rows

Subscribe to SMS Subscribe to RSS
Subscribe

This procedure will split  a “;” separated column in to new fields preserving ids.

This is very specific problem, lets check it with example.

Consider a sample table test:

source-table

And we want output as follows:

output-table

So again I wrote a procedure.

Procedure will read a data from “tmp” table and will split data by my_delimiter delimiter and generate a temporary table.

You may alter it as per requirement.

Download Stored Procedure : split_string

DELIMITER $$


DROP PROCEDURE IF EXISTS `split_string` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `split_string`()
BEGIN

DECLARE my_delimiter CHAR(1);
DECLARE split_string varchar(255);
DECLARE done INT;
DECLARE occurance INT;
DECLARE i INT;
DECLARE split_id INT;
DECLARE ins_query VARCHAR(500);
DECLARE splitter_cur CURSOR FOR
SELECT id,cat from tmp;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

DROP TEMPORARY TABLE IF EXISTS `my_splits`;
CREATE TEMPORARY TABLE `my_splits` (
`splitted_column` varchar(45) NOT NULL,
`id` int(10) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

OPEN splitter_cur;
splitter_loop:LOOP
FETCH splitter_cur INTO split_id,split_string;

SET occurance=length(split_string)-length(replace(split_string,’;',”))+1;
SET my_delimiter=’;';
IF done=1 THEN
LEAVE splitter_loop;
END IF;
#  select occurance;
IF occurance > 0 then
#select occurance;
set i=1;
while i <= occurance do
#        select concat(“SUBSTRING_INDEX(SUBSTRING_INDEX( ‘”,split_string ,”‘, ‘”,my_delimiter,”‘, “,i, “),’”,my_delimiter,”‘,-1);”);
SET ins_query=concat(“insert into my_splits(splitted_column,id) values(“, concat(“SUBSTRING_INDEX(SUBSTRING_INDEX( ‘”,split_string ,”‘, ‘”,my_delimiter,”‘, “,i, “),’”,my_delimiter,”‘,-1),”,split_id,”);”));
#    select ins_query;
set @ins_query=ins_query;
PREPARE ins_query from @ins_query;
EXECUTE ins_query;
set i=i+1;
end while;
ELSE
set ins_query=concat(“insert into my_splits(splitted_column,id) values(“,split_string,”‘,”,split_id,”);”);
set @ins_query=ins_query;
PREPARE ins_query from @ins_query;
EXECUTE ins_query;
END IF;
set occurance=0;
END LOOP;

CLOSE splitter_cur;

END $$

DELIMITER ;

Usage: call split_string();

Bookmark and Share

Related posts:

  1. Stored procedure to add-remove prefix by rename table mysql Here is one more procedure – (this time) for...
  2. Stored procedure to Find database objects This procedure lists available database objects under passed database...
  3. Ideas for select all columns but one mysql stored procedure Assume we’ve a table with 100 rows and we...

Reader Feedback

4 Responses to “MySQL Stored procedure – Split Delimited string into Rows”

  1. Marc says:

    I would love to see the screenshots that you’re linking to in this post but the links are dead. Can you relink for me?

    kedar.nitty-witty.com/blog/wp-content/uploads/2009/12/source-table.JPG

    kedar.nitty-witty.com/blog/wp-content/uploads/2009/12/output-table.jpg

    Thanks!

  2. Kedar says:

    @Marc: Thanks for pointing that out.
    Images are now fixed.

  3. Pierinux says:

    thank you very much for the tip. I’m using this technique on a project I will soon release as GPL, an opensource subscription manager for small magazines. your procedure is very useful in my project to build the delivery packing list, piking up the list of magazine numbers subscribed from a comma-separated list. I will credit your work as soon as it is ready. cheers! pierinux

Leave a Reply