I’m into writing a stored procedure which explodes a passed string by a
passed delimiter and returns the n-th element of the result. n is passed
too.
So this is what I came up with:
CREATE PROCEDURE SPLIT(IN strToSplit text, IN strDelimiter varchar(1), IN nPartToGet int,OUT strSlice varchar(255))
BEGIN
SET strSlice = replace(substring(substring_index(strToSplit, strDelimiter, nPartToGet),
length(substring_index(strToSplit,strDelimiter, nPartToGet - 1)) + 1), strDelimiter, '')
END
;
Sadly mysql keeps naging me that I’ve got an syntax error in there. IMHO this should work. Could anyone pls poke me on where I’m going wrong?
thanks in advance
K
You need to end your SET with a ‘;’ and, given that the client interprets ; as the delimiter, you need to change the delimiter so you can enter an actual ; into the procedure.
Relevant docs: http://dev.mysql.com/doc/refman/5.0/en/stored-routines.html