How to split the name string in mysql?

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

    To get the rest of the string after the second instance of the space delimiter:

    SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(MsgRest, ' ', 1), ' ', -1) AS EMailID
    ,  SUBSTRING_INDEX(SUBSTRING_INDEX(MsgRest, ' ', 2), ' ', -1) AS DOB
    ,  IF(
        LOCATE(' ', `MsgRest`) > 0,
        TRIM(SUBSTRING(SUBSTRING(`MsgRest`, LOCATE(' ', `MsgRest`) +1), 
             LOCATE(' ', SUBSTRING(`MsgRest`, LOCATE(' ', `MsgRest`) +1)) +1)),
        NULL
    ) AS Person
    FROM inbox
    
    0 讨论(0)
  • 2020-11-22 12:05

    We have stored the value of course Name and chapter name in single column ChapterName.

    Value stored like : " JAVA : Polymorphism "

    you need to retrieve CourseName : JAVA and ChapterName : Polymorphism

    Below is the SQL select query to retrieve .

           SELECT   
              SUBSTRING_INDEX(SUBSTRING_INDEX(ChapterName, ' ', 1), ' ', -1) AS 
           CourseName,
    
           REPLACE(TRIM(SUBSTR(ChapterName, LOCATE(':', ChapterName)) ),':','') AS 
           ChapterName
           FROM Courses where `id`=1;
    

    Please let me know if any question on this.

    0 讨论(0)
  • 2020-11-22 12:08

    First Create Procedure as Below:

    CREATE DEFINER=`root`@`%` PROCEDURE `sp_split`(str nvarchar(6500), dilimiter varchar(15), tmp_name varchar(50))
    BEGIN
    
        declare end_index   int;
        declare part        nvarchar(6500);
        declare remain_len  int;
    
        set end_index      = INSTR(str, dilimiter);
    
        while(end_index   != 0) do
    
            /* Split a part */
            set part       = SUBSTRING(str, 1, end_index - 1);
    
            /* insert record to temp table */
            call `sp_split_insert`(tmp_name, part);
    
            set remain_len = length(str) - end_index;
            set str = substring(str, end_index + 1, remain_len);
    
            set end_index  = INSTR(str, dilimiter);
    
        end while;
    
        if(length(str) > 0) then
    
            /* insert record to temp table */
            call `sp_split_insert`(tmp_name, str);
    
        end if;
    
    END
    

    After that create procedure as below:

    CREATE DEFINER=`root`@`%` PROCEDURE `sp_split_insert`(tb_name varchar(255), tb_value nvarchar(6500))
    BEGIN
        SET @sql = CONCAT('Insert Into ', tb_name,'(item) Values(?)'); 
        PREPARE s1 from @sql;
        SET @paramA = tb_value;
        EXECUTE s1 USING @paramA;
    END
    

    How call test

    CREATE DEFINER=`root`@`%` PROCEDURE `test_split`(test_text nvarchar(255))
    BEGIN
    
        create temporary table if not exists tb_search
            (
                item nvarchar(6500)
            );
    
        call sp_split(test_split, ',', 'tb_search');
    
        select * from tb_search where length(trim(item)) > 0;
    
        drop table tb_search;
    
    END
    
    
    call `test_split`('Apple,Banana,Mengo');
    
    0 讨论(0)
  • 2020-11-22 12:09
    SELECT
        p.fullname AS 'Fullname',
        SUBSTRING_INDEX(p.fullname, ' ', 1) AS 'Firstname',
        SUBSTRING(p.fullname, LOCATE(' ',p.fullname), 
            (LENGTH(p.fullname) - (LENGTH(SUBSTRING_INDEX(p.fullname, ' ', 1)) + LENGTH(SUBSTRING_INDEX(p.fullname, ' ', -1))))
        ) AS 'Middlename',
        SUBSTRING_INDEX(p.fullname, ' ', -1) AS 'Lastname',
        (LENGTH(p.fullname) - LENGTH(REPLACE(p.fullname, ' ', '')) + 1) AS 'Name Qt'
    FROM people AS p
    LIMIT 100; 
    

    Explaining:

    Find firstname and lastname are easy, you have just to use SUBSTR_INDEX function Magic happens in middlename, where was used SUBSTR with Locate to find the first space position and LENGTH of fullname - (LENGTH firstname + LENGTH lastname) to get all the middlename.

    Note that LENGTH of firstname and lastname were calculated using SUBSTR_INDEX

    0 讨论(0)
  • 2020-11-22 12:09
    concat(upper(substring(substring_index(NAME, ' ', 1) FROM 1 FOR 1)), lower(substring(substring_index(NAME, ' ', 1) FROM 2 FOR length(substring_index(NAME, ' ', 1))))) AS fname,
    CASE 
    WHEN length(substring_index(substring_index(NAME, ' ', 2), ' ', -1)) > 2 THEN 
      concat(upper(substring(substring_index(substring_index(NAME, ' ', 2), ' ', -1) FROM 1 FOR 1)), lower(substring(substring_index(substring_index(f.nome, ' ', 2), ' ', -1) FROM 2 FOR length(substring_index(substring_index(f.nome, ' ', 2), ' ', -1)))))
      ELSE 
      CASE 
      WHEN length(substring_index(substring_index(f.nome, ' ', 3), ' ', -1)) > 2 THEN 
        concat(upper(substring(substring_index(substring_index(f.nome, ' ', 3), ' ', -1) FROM 1 FOR 1)), lower(substring(substring_index(substring_index(f.nome, ' ', 3), ' ', -1) FROM 2 FOR length(substring_index(substring_index(f.nome, ' ', 3), ' ', -1)))))
      END 
    END 
    AS mname
    
    0 讨论(0)
  • 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 ;
    
    0 讨论(0)
提交回复
热议问题