Possible Duplicate:
Can you split/explode a field in a MySQL query?
I want to convert php explode function in mysql procedure
BUT
the explode function does not contain the length ( we do not know on which index does the delimiter comes )
AND
on the exploded array run a loop as in the php code I write for loop on that exploded array
PHP Code
$data = 'Computers|Personal Computer|Home Use|Accessories|Mouse|Wireless Mouse';
$categories = explode("|", $data);
for ( $i = 0; $i < sizeof($categories); $i++ ){
$this->insert_category( $categories[$i] );
}
I used the function below in mysql but it only returns a single value but I want an array also this function wants a specfic index which in my case is not known because data will be of any type.
Mysql
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '');
I have also used these function just to make some thing closer to my result but the functions are not behaving accurately e.g they are not spliting correctly, not returning correct index etc,
i am using all the functions below one by one or with a combination not all at the same time
SET temp2 = substring_index(data_mysql, '|', 2);
SET temp2 = find_in_set('|', data_mysql);
SET temp2 = instr(data_mysql, '|');
SET temp3 = SPLIT_STR(data_mysql, '|', temp2 );
I declared the variables in the mysql procedure as
DECLARE data_mysql VARCHAR(200) DEFAULT NULL;
DECLARE temp1 VARCHAR(150) DEFAULT NULL;
DECLARE temp2 VARCHAR(150) DEFAULT NULL;
DECLARE temp3 VARCHAR(150) DEFAULT NULL;