Created
October 20, 2014 16:40
-
-
Save molotovbliss/fe5a86ee58f6bfb51e9c to your computer and use it in GitHub Desktop.
SQL Function: PHP explode similar
This file contains 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
/** | |
* Split a string by string (Similar to the php function explode()) | |
* | |
* @param VARCHAR(12) delim The boundary string (delimiter). | |
* @param VARCHAR(255) str The input string. | |
* @param INT pos The index of the string to return | |
* @return VARCHAR(255) The (pos)th substring | |
* @return VARCHAR(255) Returns the [pos]th string created by splitting the str parameter on boundaries formed by the delimiter. | |
* @{@example | |
* SELECT SPLIT_STRING('|', 'one|two|three|four', 1); | |
* This query | |
* } | |
*/ | |
DROP FUNCTION IF EXISTS SPLIT_STRING; | |
CREATE FUNCTION SPLIT_STRING(delim VARCHAR(12), str VARCHAR(255), pos INT) | |
RETURNS VARCHAR(255) DETERMINISTIC | |
RETURN | |
REPLACE( | |
SUBSTRING( | |
SUBSTRING_INDEX(str, delim, pos), | |
LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1 | |
), | |
delim, '' | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment