MYSQL 提取字符串中的数字

方法一:

目标表contract_service_content数据实例

 

 配置表configdb的数据实例,如果有权限访问数据库的  mysql.help_topic 表 则不用创建配置表 直接用mysql.help_topic表中的help_topic_id,不然就要创建一个表ID连续的配置表

 

 

select spc_id,fee,cast(group_concat(c order by pos separator '') as unsigned)
         as mixed1
  from (
    /*分割fee字符串 并将将字符的asc码在48到57之间的提取出来(也就是数字)*/
select v.spc_id , v.fee, iter.pos,substr(v.fee,iter.pos,1) as c
  from contract_service_content v,
       ( select id pos from configdb ) iter
where iter.pos <= length(v.fee)
and   ascii(substr(v.fee,iter.pos,1)) between 48 and 57
      ) y
group by spc_id,fee
order by spc_id

运行结果:

 

 此方法参考:https://www.jianshu.com/p/289953083796

方法二:

select CAST('21岁' as SIGNED);
                                
select  CONVERT('021岁', SIGNED)

上面两条sql 运行结果都是21

此方法明细参考 https://www.cnblogs.com/baby123/p/11716896.html 有详细介绍

 

 

方法一  可以提取提取字符串中的所有数字

方法二只能提取到字符串前面的数字,有局限性

posted @ 2022-03-23 08:47  求路问道  阅读(4320)  评论(0编辑  收藏  举报