Lebowtech Blog

MySQL Split String Function

| Comments

MySQL does not include a function to split a delimited string. However, it’s very easy to create your own function.

SPLIT_STR FUNCTIONlink
1
2
3
4
5
6
7
8
9
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, '');

Comments