Skip to content

Instantly share code, notes, and snippets.

@pierophp
Created July 13, 2016 15:23
Show Gist options
  • Save pierophp/5a1e85767019ff71c8326a7c79bb6c9a to your computer and use it in GitHub Desktop.
Save pierophp/5a1e85767019ff71c8326a7c79bb6c9a to your computer and use it in GitHub Desktop.
delimiter $$
CREATE FUNCTION `SPLIT_STR`(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
) RETURNS varchar(255) CHARSET utf8 COLLATE utf8_unicode_ci
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
CHAR_LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '')
$$
@pierophp
Copy link
Author

Usage:

SELECT SPLIT_STR('My String', ' ', 1);

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