MySQL Second (or third) Index Of in String

后端 未结 5 1496
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 18:19

What would be the simplest way to locate the index of the third space in a string.

My goal is to get CCC out of this space separated list: AAAA BB

5条回答
  •  天命终不由人
    2020-12-15 19:00

    
    DROP FUNCTION IF EXISTS `Find_string_by_position`$$
    
    CREATE DEFINER=`root`@`localhost` FUNCTION
    `Find_string_by_position`(str VARCHAR(255), delimeter VARCHAR(255),pos INT(2)) RETURNS VARCHAR(255) CHARSET utf8mb4 BEGIN
           DECLARE s VARCHAR(255);  
           DECLARE d VARCHAR(255); 
           DECLARE p INT DEFAULT 1;
           DECLARE val VARCHAR(255);
    
    
           SET s = LCASE(str); 
           SET d = delimeter;
           SET p = pos;
           SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(s,d,p),d,-1) INTO @val; 
    
           RETURN @val;  
    
       END$$
    
    DELIMITER ;
    
    

提交回复
热议问题