duanlipeng4136 2017-07-19 04:25
浏览 166
已采纳

Mysql拆分字符串并获取整数值

I have trouble to split string and get only numeric value those are in round bracket. I try substring_index but can't get success.

Column

prd_code

HWC-4054 (100 Pcs available)
HWC-7514 (125 pcs available)
HWC-1516 (total 80 pcs available)
HWC-8008 (80pcs available)

Required output

prd_code

100
125

Thank you.

  • 写回答

4条回答 默认 最新

  • doutan5337 2017-07-19 05:46
    关注

    Please check below solutions using mysql function.

    select prd_code,digits(SUBSTRING_INDEX( SUBSTRING_INDEX( prd_code , '(', -1 ), ')', 1)) as one from `your table`
    

    use below mysql function :

    DELIMITER |
    CREATE FUNCTION digits( str CHAR(32) ) RETURNS CHAR(32)
    BEGIN
     DECLARE i, len SMALLINT DEFAULT 1;
     DECLARE ret CHAR(32) DEFAULT '';
     DECLARE c CHAR(1);
    
     IF str IS NULL
     THEN 
       RETURN "";
     END IF;
    
     SET len = CHAR_LENGTH( str );
     REPEAT
       BEGIN
         SET c = MID( str, i, 1 );
         IF c BETWEEN '0' AND '9' THEN 
           SET ret=CONCAT(ret,c);
         END IF;
         SET i = i + 1;
       END;
     UNTIL i > len END REPEAT;
     RETURN ret;
    END |
    DELIMITER ;
    

    reference from this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?