How to split the name string in mysql?

后端 未结 16 1976
轮回少年
轮回少年 2020-11-22 11:44

How to split the name string in mysql ?

E.g.:

name
-----
Sachin ramesh tendulkar
Rahul dravid

Split the name like firstname

16条回答
  •  太阳男子
    2020-11-22 12:13

    There is no string split function in MySQL. so you have to create your own function. This will help you. More details at this link.

    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   |
    +-------+
    

提交回复
热议问题