Skip to content

Instantly share code, notes, and snippets.

@devacto
Last active December 27, 2015 12:59
Show Gist options
  • Save devacto/7330256 to your computer and use it in GitHub Desktop.
Save devacto/7330256 to your computer and use it in GitHub Desktop.
Various useful MySQL string functions. (Original source: http://goo.gl/udse6R)
CREATE FUNCTION strSplit(x varchar(255), delim varchar(12), pos int) returns varchar(255)
return replace(substring(substring_index(x, delim, pos), length(substring_index(x, delim, pos - 1)) + 1), delim, '');
@devacto
Copy link
Author

devacto commented Nov 6, 2013

select strSplit("aaa,b,cc,d", ',', 2) as second;
+--------+
| second |
+--------+
| b |
+--------+

select strSplit("a|bb|ccc|dd", '|', 3) as third;
+-------+
| third |
+-------+
| ccc |
+-------+

select strSplit("aaa,b,cc,d", ',', 7) as 7th;
+------+
| 7th |
+------+
| NULL |
+------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment