mysql 去掉字符串中的非数字

最简单使用mysql自带的正则替换函数: regexp_replace (值,正则表达式,'')

测试sql: column_name='Aabc6.6e f s'  结果:6.6

SELECT regexp_replace ("Aabc6.6e f s",'[^0-9.]+','')

注意:regexp_replace 函数为 mysql8.0 版本新增,低于8.0版本没有此函数,又不想写自定义函数; 所以自己写了一个处理sql

sql代码

    select GROUP_CONCAT(if(ascii(substring(column_name,help_topic_id+1,1))
    in ('46','48','49','50','51','52','53','54','55','56','57')
    ,substring(column_name,help_topic_id+1,1),'') separator '')
    from mysql.help_topic where help_topic_id < length(column_name)

 测试sql: column_name='Aabc6.6e f s'  结果:6.6

	select GROUP_CONCAT(if(ascii(substring('Aabc6.6e f s',help_topic_id+1,1))
    in ('46','48','49','50','51','52','53','54','55','56','57')
    ,substring('Aabc6.6e f s',help_topic_id+1,1),'') separator '')
    from mysql.help_topic where help_topic_id < length('Aabc6.6e f s')
		
		

生产中查询替换某个列的非数字sql:

	select  tp.column_name /* 原字段值 */

    ,(select GROUP_CONCAT(if(ascii(substring(tp.column_name,help_topic_id+1,1))
    in ('46','48','49','50','51','52','53','54','55','56','57')
    ,substring(tp.column_name,help_topic_id+1,1),'') separator '')
    from mysql.help_topic where help_topic_id < length(tp.column_name)) num  /* 处理后字段值 */

    from (select 'Aabc6.6e f s' column_name ) tp /* 测试的子查询表 */

去掉字符串中的数字,或处理其他的也可以这种模式修改

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值