
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Split a string and loop through values in MySQL Procedure?
To split a string and loop through all values in MySQL procedure, you do not need to use REPLACE() function. To understand, first create a stored procedure and after that create a table and call the stored procedure with some values. The value will be inserted into the table.
The query to create a stored procedure is as follows:
mysql> DELIMITER // mysql> CREATE PROCEDURE SP_SplitString(Value longtext) -> BEGIN -> DECLARE front TEXT DEFAULT NULL; -> DECLARE frontlen INT DEFAULT NULL; -> DECLARE TempValue TEXT DEFAULT NULL; -> iterator: -> LOOP -> IF LENGTH(TRIM(Value)) = 0 OR Value IS NULL THEN -> LEAVE iterator; -> END IF; -> SET front = SUBSTRING_INDEX(Value,',',1); -> SET frontlen = LENGTH(front); -> SET TempValue = TRIM(front); -> INSERT INTO store (allValues) VALUES (TempValue); -> SET Value = INSERT(Value,1,frontlen + 1,''); -> END LOOP; -> END // Query OK, 0 rows affected (0.22 sec) mysql> DELIMITER ;
Now create a table that stores the value of stored procedure. The query to create a table.
mysql> CREATE TABLE store( -> Id int NOT NULL AUTO_INCREMENT, -> allValues varchar(30), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.63 sec)
Display all records from the table. The query is as follows:
mysql> select *from store; Empty set (0.00 sec)
Initially we do not have any records in the table. Let us call the stored procedure using CALL command. The query is as follows:
mysql> call SP_SplitString('Hi,Hello,Good Morning,Bye'); Query OK, 1 row affected (1.02 sec)
After calling the above stored procedure, let us check once again the table records. The query to display all records from the table is as follows:
mysql> select *from store;
The following is the output:
+----+--------------+ | Id | allValues | +----+--------------+ | 1 | Hi | | 2 | Hello | | 3 | Good Morning | | 4 | Bye | +----+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to loop thrugh a stored procedure in MySQL?
- Split a string and insert it as individual values into a MySQL table?
- Loop through array and edit string JavaScript
- How to use FOR LOOP in MySQL Stored Procedure?
- How Can MySQL LOOP statement be used in a stored procedure?
- How MySQL WHILE loop statement can be used in stored procedure?
- How MySQL REPEAT loop statement can be used in stored procedure?
- MySQL Stored Procedure calling with INT and VARCHAR values
- How to correctly use delimiter in a MySQL stored procedure and insert values?
- PHP preg_split to split a string with specific values?
- Split the left part of a string by a separator string in MySQL?
- How can I loop through all rows of a table in MySQL?
- How to loop through all values of an enum in C#?
- How to split string in MySQL using SUBSTRING_INDEX?
- Loop through a Dictionary in Javascript
