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?
Asked
Active
Viewed 2,129 times
1
-
Not an answer to your Q but you should always try to keep only a single piece of data in any one field/record. In this case, how about storing the P seperately from the 12? – Basic Apr 03 '12 at 08:23
-
It seems this question was asked before: http://stackoverflow.com/questions/471914/can-you-split-explode-a-field-in-a-mysql-query – Bram Apr 03 '12 at 08:21
3 Answers
2
Here are many discussion about the SPLIT
problem in mysql :
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

BMN
- 8,253
- 14
- 48
- 80
1
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 |
+-------+

Naveen Kumar
- 4,543
- 1
- 18
- 36
0
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

Russell Harkins
- 404
- 3
- 4