I saw a question about how to split delimited string in MySQL. MySQL does not include a function to split a delimited string. So I decided to write a simple one:
1 2 3 4 5 6 7 8 9 10 |
DELIMITER $$ CREATE FUNCTION split( str VARCHAR(500), delchar VARCHAR(2), x INT ) RETURNS VARCHAR(500) BEGIN RETURN SUBSTR(SUBSTRING_INDEX(str, delchar, x), LENGTH(SUBSTRING_INDEX(str, delchar, x-1))+IF(x > 1, 2, 1)); END$$ DELIMITER ; |
When we call this function as SPLIT( ‘ali,ahmet,mehmet’, ‘,’, 3 ), we get the 3rd item “mehmet”. If we call this function as SPLIT( ‘ali,ahmet,mehmet’, ‘,’, 5 ), it will return an empty string.
mahesh singh
Gokhan Atil