In this post, I am sharing one user defined function to select a particular value from comma separated string in the MySQL.
In our MySQL Database, we are managing some of the table in which we are storing dynamic content management system (CMS) related data.
As per the requirement, we are also storing comma delimited values in some the tables.
Now the requirement is to find third value from each comma separated string.
I have created one user defined function to solve this problem.
First, Create UDF to split and select a particular value from comma separated string:
Three parameters require: Delimited_String, Delimited_Character, and Position.
CREATE FUNCTION fn_Get_Split_String
REPLACE(SUBSTRING(SUBSTRING_INDEX(DelimitedString, DelimitedCharacter, Position)
,LENGTH(SUBSTRING_INDEX(DelimitedString, DelimitedCharacter, Position-1)) + 1)
Create a table with sample data:
CREATE TABLE tbl_CommaSeparated
INSERT INTO tbl_CommaSeparated
Using UDF, SELECT second value from each comma separated string:
SELECT fn_Get_Split_String(CommaDelimited,',',2) AS ResultData
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.