How to split the name string in mysql?

后端 未结 16 1948
轮回少年
轮回少年 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 11:48

    You can use bewlo one also:

    SELECT SUBSTRING_INDEX(Name, ' ', 1) AS fname,
    SUBSTRING_INDEX(SUBSTRING_INDEX(Name,' ', 2), ' ',-1) AS mname,
    SUBSTRING_INDEX(Name, ' ', -1) as lname FROM mytable;
    
    0 讨论(0)
  • 2020-11-22 11:48

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

    SELECT
       SUBSTRING_INDEX(SUBSTRING_INDEX('Sachin ramesh tendulkar', ' ', 1), ' ', -1) AS first_name, 
           SUBSTRING_INDEX(SUBSTRING_INDEX('Sachin ramesh tendulkar', ' ', 2), ' ', -1) 
               AS middle_name,
       SUBSTRING('Sachin ramesh tendulkar',LENGTH(SUBSTRING_INDEX('Sachin ramesh tendulkar', ' ', 2))+1) AS last_name
    
    0 讨论(0)
  • 2020-11-22 11:48
    CREATE DEFINER=`root`@`localhost` FUNCTION `getNameInitials`(`fullname` VARCHAR(500), `separator` VARCHAR(1)) RETURNS varchar(70) CHARSET latin1
        DETERMINISTIC
    BEGIN
    DECLARE `result` VARCHAR(500) DEFAULT '';
    DECLARE `position` TINYINT;
    
    
    
    SET `fullname` = TRIM(`fullname`);
    
    SET `position` = LOCATE(`separator`, `fullname`);
    
    IF NOT `position`
    THEN RETURN LEFT(`fullname`,1);
    END IF;
    
    SET `fullname` = CONCAT(`fullname`,`separator`);
    SET `result` = LEFT(`fullname`, 1);
    
    cycle: LOOP
        SET `fullname` = SUBSTR(`fullname`, `position` + 1);
        SET `position` = LOCATE(`separator`, `fullname`);
    
        IF NOT `position` OR NOT LENGTH(`fullname`)
        THEN LEAVE cycle;
        END IF;
    
        SET `result` = CONCAT(`result`,LEFT(`fullname`, 1));
       -- SET `result` = CONCAT_WS(`separator`, `result`, `buffer`);
    END LOOP cycle;
    
    RETURN upper(`result`);
    END
    

    1.Execute this function in mysql. 2.this will create a function. Now you can use this function anywhere you want.

     SELECT `getNameInitials`('Kaleem Ul Hassan', ' ') AS `NameInitials`;
    

    3. The above getNameInitails first parameter is string you want to filter and second is the spectator character on which you want to separate you string. 4. In above example 'Kaleem Ul Hassan' is name and i want to get initials and my separator is space ' '.

    0 讨论(0)
  • 2020-11-22 11:48

    DELIMITER $$
    
    DROP FUNCTION IF EXISTS `split_name`$$
    
    CREATE FUNCTION split_name (p_fullname TEXT, p_part INTEGER)
    RETURNS TEXT
        READS SQL DATA
    BEGIN
        DECLARE v_words INT UNSIGNED;
        DECLARE v_name TEXT;
    
        SET p_fullname=RTRIM(LTRIM(p_fullname));
    
        SET v_words=(SELECT SUM(LENGTH(p_fullname) - LENGTH(REPLACE(p_fullname, ' ', ''))+1));
    
        IF v_words=1 THEN 
            IF p_part=1 THEN
                SET v_name=p_fullname;
            ELSEIF p_part=2 THEN
                SET v_name=NULL;
            ELSEIF p_part=3 THEN
                SET v_name=NULL;
            ELSE
                SET v_name=NULL;
            END IF; 
        ELSEIF v_words=2 THEN 
            IF p_part=1 THEN
                SET v_name=SUBSTRING(p_fullname, 1, LOCATE(' ', p_fullname) - 1);
            ELSEIF p_part=2 THEN
                SET v_name=SUBSTRING(p_fullname, LOCATE(' ', p_fullname) + 1);
            ELSEIF p_part=3 THEN
                SET v_name=NULL;
            ELSE
                SET v_name=NULL;
            END IF; 
        ELSEIF v_words=3 THEN 
            IF p_part=1 THEN
                SET v_name=SUBSTRING(p_fullname, 1, LOCATE(' ', p_fullname) - 1);
            ELSEIF p_part=2 THEN
                SET p_fullname=SUBSTRING(p_fullname, LOCATE(' ', p_fullname) + 1);
                SET v_name=SUBSTRING(p_fullname, 1, LOCATE(' ', p_fullname) - 1);
            ELSEIF p_part=3 THEN
                SET p_fullname=REVERSE (SUBSTRING(p_fullname, LOCATE(' ', p_fullname) + 1));
                SET p_fullname=SUBSTRING(p_fullname, 1, LOCATE(' ', p_fullname) - 1);
                SET v_name=REVERSE(p_fullname);
            ELSE
                SET v_name=NULL;
            END IF; 
        ELSEIF v_words>3 THEN 
            IF p_part=1 THEN
                SET v_name=SUBSTRING(p_fullname, 1, LOCATE(' ', p_fullname) - 1);
            ELSEIF p_part=2 THEN
                SET p_fullname=REVERSE(SUBSTRING(p_fullname, LOCATE(' ', p_fullname) + 1));
                SET p_fullname=SUBSTRING(p_fullname, LOCATE(' ', p_fullname,SUBSTRING_INDEX(p_fullname,' ',1)+1) + 1);
                SET v_name=REVERSE(p_fullname);
            ELSEIF p_part=3 THEN
                SET p_fullname=REVERSE (SUBSTRING(p_fullname, LOCATE(' ', p_fullname) + 1));
                SET p_fullname=SUBSTRING(p_fullname, 1, LOCATE(' ', p_fullname) - 1);
                SET v_name=REVERSE(p_fullname);
            ELSE
                SET v_name=NULL;
            END IF;
        ELSE
            SET v_name=NULL;
        END IF;
     RETURN v_name; 
    END;
    

    SELECT split_name('Md. Obaidul Haque Sarker',1) AS first_name,
    split_name('Md. Obaidul Haque Sarker',2) AS middle_name,
    split_name('Md. Obaidul Haque Sarker',3) AS last_name
    
    0 讨论(0)
  • 2020-11-22 11:54

    I've seperated this answer into two(2) methods. The first method will separate your fullname field into first, middle, and last names. The middle name will show as NULL if there is no middle name.

    SELECT
       SUBSTRING_INDEX(SUBSTRING_INDEX(fullname, ' ', 1), ' ', -1) AS first_name,
       If(  length(fullname) - length(replace(fullname, ' ', ''))>1,  
           SUBSTRING_INDEX(SUBSTRING_INDEX(fullname, ' ', 2), ' ', -1) ,NULL) 
               as middle_name,
       SUBSTRING_INDEX(SUBSTRING_INDEX(fullname, ' ', 3), ' ', -1) AS last_name
    FROM registeredusers
    

    This second method considers the middle name as part of the lastname. We will only select a firstname and lastname column from your fullname field.

    SELECT
       SUBSTRING_INDEX(SUBSTRING_INDEX(fullname, ' ', 1), ' ', -1) AS first_name,
        TRIM( SUBSTR(fullname, LOCATE(' ', fullname)) ) AS last_name
    FROM registeredusers
    

    There's a bunch of cool things you can do with substr, locate, substring_index, etc. Check the manual for some real confusion. http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

    0 讨论(0)
  • 2020-11-22 11:56

    Well, nothing I used worked, so I decided creating a real simple split function, hope it helps:

    DECLARE inipos INTEGER;
    DECLARE endpos INTEGER;
    DECLARE maxlen INTEGER;
    DECLARE item VARCHAR(100);
    DECLARE delim VARCHAR(1);
    
    SET delim = '|';
    SET inipos = 1;
    SET fullstr = CONCAT(fullstr, 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           
            USE_THE_ITEM_STRING;
        END IF;
        SET inipos = endpos + 1;
    UNTIL inipos >= maxlen END REPEAT;
    
    0 讨论(0)
提交回复
热议问题