Is there any function in MySQL to explode data of a column and then retrieve it? Like if a column data is P:12 , then can the data be exploded on \':\' and then read?
I parse the string using a while loop and insert the split items into a temporary table. It's complicated, but I copy and paste the code, then change the table name for readability. Here's an example to parse a comma delimited list of user ids into a temp table:
CREATE PROCEDURE spParse
(_UserList MEDIUMTEXT)
BEGIN
DECLARE _Pos INT;
DECLARE _Start INT;
DECLARE _Item INT;
DECLARE _Length INT;
CREATE TEMPORARY TABLE IF NOT EXISTS TempUserList
(
UserID INT
);
SET _Length = LENGTH(_UserList);
SET _Pos = 1;
SET _Start = 1;
divide_loop:LOOP
IF _Pos > _Length Then
LEAVE divide_loop;
End If;
IF SUBSTRING(_UserList,_Pos,1) = ',' Then
IF _Pos - _Start > 0 Then
IF IsNumeric(SUBSTRING(_UserList,_Start,_Pos-_Start)) Then
SET _Item = CONVERT(SUBSTRING(_UserList,_Start,_Pos-_Start),Signed);
INSERT INTO TempUserList (UserID)
VALUES (_Item);
End If;
End If;
SET _Start = _Pos + 1;
End If;
SET _Pos = _Pos + 1;
END LOOP divide_loop;
IF _Start <= _Length Then
If IsNumeric(SUBSTRING(_UserList,_Start,_Length - _Start + 1)) Then
SET _Item = CONVERT(SUBSTRING(_UserList,_Start,_Length - _Start + 1),Signed);
INSERT INTO TempUserList (UserID)
VALUES (_Item);
End If;
End If;
SELECT UserID FROM TempUserList;
DROP TABLE TempUserList;
END
Here are many discussion about the SPLIT
problem in mysql :
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
You can write a Split function in MYSQL check this link
From the Link
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)
Example
SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;
+-------+
| third |
+-------+
| ccc |
+-------+