split() function in mysql

Posted by: Brad Waite

Tagged in: Untagged 

The following function definition creates a split() function that splits a string by a given delimiter and returns the specified element.

DROP FUNCTION IF EXISTS split;
CREATE FUNCTION split (str TEXT, delim VARCHAR(1), N INT)
RETURNS TEXT DETERMINISTIC
RETURN SUBSTRING(SUBSTRING_INDEX(str, delim, N), LENGTH(SUBSTRING_INDEX(str, delim, N - 1)) + LENGTH(delim) + (N > 1))

The syntax of the function is as follows.

split(str, separator, pos)
Comments (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy