Federico Cargnelutti

Simple is better than complex. Complex is better than complicated. | @fedecarg

MySQL Split String Function

with 17 comments

MySQL does not include a function to split a delimited string. However, it’s very easy to create your own function.

Create function syntax

A user-defined function is a way to extend MySQL with a new function that works like a native MySQL function.

CREATE [AGGREGATE] FUNCTION function_name
RETURNS {STRING|INTEGER|REAL|DECIMAL}

To create a function, you must have the INSERT privilege for the <mysql> database.

Split delimited strings

The following example function takes 3 parameters, performs an operation using an SQL function, and returns the result.

Function

CREATE FUNCTION SPLIT_STR(
  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, '');

Usage

SELECT SPLIT_STR(string, delimiter, position)

Example

SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;

+-------+
| third |
+-------+
| ccc   |
+-------+

Written by Federico

February 22, 2009 at 12:11 pm

Posted in Databases

17 Responses

Subscribe to comments with RSS.

  1. Worth noting that if you write an application that depends on a custom function like this, you should definitely let your system administrator know. I believe that the function definition is stored in the information_schema database on mysql, and if your system administrator isn’t backing up that database, you might get a nasty surprise if he ever has to restore from backups.

    karlkatzke

    February 23, 2009 at 1:39 am

  2. True, thanks for pointing that out.

    Federico

    February 23, 2009 at 10:23 am

  3. Have I read “worth nothing” ???
    That’s a great function, very useful !!
    Thanks for sharing Fédérico ! :-)

    Erwan

    March 24, 2009 at 1:26 pm

  4. It may be nice to have the function stored if you think you will re-use it! I think I can cut your code like this:
    SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(‘a|bb|ccc|dd’, ‘|’, 3), “|”, -1);
    Only the value of “3″ needs to be changed to select an item in the list. “-1″ will never change.

    Damien Goor

    March 27, 2009 at 3:14 pm

  5. SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(‘a|bb|ccc|dd’, ‘|’, 9), “|”, -1);

    dd

    One might want check that the desired number of delimiters exists as well, e.g.:

    SELECT ‘a, bb, ccc, dd’ RLIKE ‘((, ).*){3}’;

    1

    SELECT ‘a, bb, ccc, dd’ RLIKE ‘((, ).*){4}’;

    0

    Jussi

    March 30, 2009 at 11:37 am

  6. Thanks!!!!!

    PEGG

    March 30, 2009 at 1:24 pm

  7. very helpful! thanks a lot!

    randell

    June 8, 2009 at 2:27 pm

  8. Nice one! Thanks a lot!

    Anton

    August 7, 2009 at 8:39 am

  9. Very useful!

    rob

    September 11, 2009 at 7:06 am

  10. Nice function. Thanks

    orice on

    October 19, 2009 at 9:32 am

  11. Thanx dear u solved my big problem thanx again.:-)

    UFM

    October 25, 2009 at 6:52 am

  12. LENGTH(.. -> CHAR_LENGTH(..

    gsa

    November 25, 2009 at 3:41 pm

  13. Very helpful!!!! Thanks a bunch.

    Deepti

    February 18, 2010 at 1:19 pm

  14. Hello, very good function, do you allow me to write a post about this on my Blog with a reference to your Post at the end?

    Alessandro De Carli

    June 12, 2010 at 8:06 am

  15. Yes, sure.

    Federico

    June 12, 2010 at 9:11 am

  16. Very nice trick

    Santhosh

    August 19, 2010 at 11:12 am


Leave a Reply