I'm currently working on a function in MYSQL, I have a comma delimited string (1,22,344,55) from another table, How can I split this in MYSQL to an array (NOT temp_table). Also, is there a similar function in MYSQL where I can do foreach()?
MySQL does not include a function to split a delimited string. However, it’s very easy to create your own function.
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, '');
Usage
SELECT SPLIT_STR(string, delimiter, position)
From here: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
The correct usage in MySQL is:
SELECT FIND_IN_SET('b','a,b,c,d');
Gives as result: 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With