如果字符串中包含数字,则仅返回数字 [英] Return Only number from a string if it contains numbers

查看:107
本文介绍了如果字符串中包含数字,则仅返回数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,
字符串是 abc123CD 需要找出一种方法来仅读取字符串
i中的数字

For example,
string is abc123CD need to find out a method to read only numbers in the string
i.e.

  select a_postgres_function('abc123CD')

  ------
  Result
  ------
  123






我的尝试

 select substring('abc123CD' from '%#"^[0-9]+$#"%' for '#')


推荐答案

尝试一下:

select (regexp_matches('abc123CD', '\d+'))[1];

由于 regexp_matches 返回文本数组,因此您应该通过 [1] 访问第一个元素。

Since regexp_matches returns array of text, you should access the first element by [1].

这篇关于如果字符串中包含数字,则仅返回数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆