Skip to content

Instantly share code, notes, and snippets.

@mikedamage
Created January 15, 2010 03:24
Show Gist options
  • Select an option

  • Save mikedamage/277782 to your computer and use it in GitHub Desktop.

Select an option

Save mikedamage/277782 to your computer and use it in GitHub Desktop.

Revisions

  1. mikedamage created this gist Jan 15, 2010.
    24 changes: 24 additions & 0 deletions split_str.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    -- SPLIT_STR MySQL Function
    -- from http://blog.fedecarg.com/2009/02/22/mysql-split-string-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, '');

    /*
    Example:
    SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;
    +-------+
    | third |
    +-------+
    | ccc |
    +-------+
    */