Search
 
SCRIPT & CODE EXAMPLE
 

SQL

mysql split explode

DELIMITER $$

DROP PROCEDURE IF EXISTS explode_table $$
CREATE PROCEDURE explode_table(bound VARCHAR(255))

  BEGIN

    DECLARE id INT DEFAULT 0;
    DECLARE value TEXT;
    DECLARE occurance INT DEFAULT 0;
    DECLARE i INT DEFAULT 0;
    DECLARE splitted_value INT;
    DECLARE done INT DEFAULT 0;
    DECLARE cur1 CURSOR FOR SELECT table1.id, table1.value
                                         FROM table1
                                         WHERE table1.value != '';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

    DROP TEMPORARY TABLE IF EXISTS table2;
    CREATE TEMPORARY TABLE table2(
    `id` INT NOT NULL,
    `value` VARCHAR(255) NOT NULL
    ) ENGINE=Memory;

    OPEN cur1;
      read_loop: LOOP
        FETCH cur1 INTO id, value;
        IF done THEN
          LEAVE read_loop;
        END IF;

        SET occurance = (SELECT LENGTH(value)
                                 - LENGTH(REPLACE(value, bound, ''))
                                 +1);
        SET i=1;
        WHILE i <= occurance DO
          SET splitted_value =
          (SELECT REPLACE(SUBSTRING(SUBSTRING_INDEX(value, bound, i),
          LENGTH(SUBSTRING_INDEX(value, bound, i - 1)) + 1), ',', ''));

          INSERT INTO table2 VALUES (id, splitted_value);
          SET i = i + 1;

        END WHILE;
      END LOOP;

      SELECT * FROM table2;
    CLOSE cur1;
  END; $$
Comment

PREVIOUS NEXT
Code Example
Sql :: cardinality example sql 
Sql :: ORA-06502: PL/SQL: numeric or value error: character string buffer too small 
Sql :: how do you insert boolean to postgresql 
Sql :: nested query 
Sql :: indexing in mysql 
Sql :: join multiple tables in sql same table 
Sql :: sql logo 
Sql :: jsonb 
Sql :: in sqlalchemy 
Sql :: query params sql insert python f string 
Sql :: mariadb errno 121 
Sql :: sqlcmd xml output insert line break after every 2033 characters 
Sql :: mysql isshow 
Sql :: delete from table and truncate table 
Sql :: First Step in installing SQL workbench 
Sql :: sql tablo hangi sp de 
Sql :: storing RGBA in mysql db 
Sql :: homebrew nysql launch 
Sql :: select * from mysql.proc 
Sql :: how to see table associated with a schema in sql 
Sql :: oracle sql developer script output limit 
Sql :: mysql Puede ser solamente un campo automatico y este debe ser definido como una clave 
Sql :: mysl like insert a variable 
Sql :: providername system.data. mysql 
Sql :: SQL Injection Using Multiple Statement 
Sql :: debian 10 install postgresql 2ndquadrant 
Sql :: why we have to set the password for my sql server 
Sql :: ring SQLite sqlite_execute 
Sql :: SQL FULL OUTER JOIN With AS Alias 
Sql :: syntac for creating view in dbbrowser 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =