Last active
September 24, 2015 10:54
-
-
Save populov/820fd7d5fc9512d3dc49 to your computer and use it in GitHub Desktop.
Analog of explode for MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DROP PROCEDURE IF EXISTS explode; | |
| CREATE PROCEDURE explode (commaDelimitedString varchar(1000)) | |
| BEGIN | |
| DECLARE rest, v varchar(1000); | |
| DECLARE comaLocation INT(11); | |
| SET rest = commaDelimitedString; | |
| CREATE TEMPORARY TABLE IF NOT EXISTS tmpExplode (`val` varchar(250)); | |
| SET comaLocation = LOCATE(',', rest); | |
| /*SELECT rest AS restFirst, comaLocation AS comaFirst;*/ | |
| WHILE comaLocation > 0 DO | |
| SET v = (SELECT RTRIM(LTRIM(SUBSTRING(rest, 1, comaLocation-1)))); | |
| INSERT INTO tmpExplode (val) | |
| VALUES (v); | |
| SET rest = SUBSTRING(rest, comaLocation+1); | |
| /*SELECT v AS testv, rest, SUBSTRING(rest, 1, comaLocation-1);*/ | |
| SET comaLocation = LOCATE(',', rest); | |
| /*SELECT rest AS restIn, comaLocation AS comaIn;*/ | |
| END WHILE; | |
| /*SELECT rest;*/ | |
| INSERT INTO tmpExplode (val) | |
| VALUES (rest); | |
| SELECT val FROM tmpExplode; | |
| DROP TABLE tmpExplode; | |
| END; | |
| CALL explode('abc,s,l') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment