How to split the name string in mysql?

后端 未结 16 2303
轮回少年
轮回少年 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:09

    Combined a few answers here to create a SP that returns the parts of the string.

    drop procedure if exists SplitStr;
    DELIMITER ;;
    CREATE PROCEDURE `SplitStr`(IN Str VARCHAR(2000), IN Delim VARCHAR(1))  
        BEGIN
            DECLARE inipos INT;
            DECLARE endpos INT;
            DECLARE maxlen INT;
            DECLARE fullstr VARCHAR(2000);
            DECLARE item VARCHAR(2000);
            create temporary table if not exists tb_split
            (
                item varchar(2000)
            );
    
    
    
            SET inipos = 1;
            SET fullstr = CONCAT(Str, delim);
            SET maxlen = LENGTH(fullstr);
    
            REPEAT
                SET endpos = LOCATE(delim, fullstr, inipos);
                SET item =  SUBSTR(fullstr, inipos, endpos - inipos);
    
                IF item <> '' AND item IS NOT NULL THEN           
                    insert into tb_split values(item);
                END IF;
                SET inipos = endpos + 1;
            UNTIL inipos >= maxlen END REPEAT;
    
            SELECT * from tb_split;
            drop table tb_split;
        END;;
    DELIMITER ;
    

提交回复
热议问题