sqlchild
sqlchild

Reputation: 9064

Function resembling the work of Explode in MySQL

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?

Upvotes: 1

Views: 2135

Answers (2)

Naveen Kumar
Naveen Kumar

Reputation: 4591

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   |
+-------+

Upvotes: 1

BMN
BMN

Reputation: 8508

Here are many discussion about the SPLIT problem in mysql :

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

Upvotes: 2

Related Questions