Oracle SQL regexp_substr 查找字符串中的数字
Oracle SQL regexp_substr to find the numbers in a string
我想使用 regexp_substr 到 return 字符串值
中字符后的数字
例如。
字符串值 return
100AUS50 50
100澳元100 100
100AUS500 500
1-20 20
四 200 200
您可以使用以下方法获取号码
select regexp_substr(column,'\d+$') from TABLE;
这是使用专门的标准字符串函数(无正则表达式)执行此操作的一种方法。 rtrim
函数只是 删除 输入字符串末尾的数字。然后 replace
从原始字符串中替换该字符串(输入字符串 减去 末尾的数字)——因此在输出中留下 "digits at the end"。
with
sample_data (str) as (
select '100AUS50' from dual union all
select '100AUS100' from dual union all
select '100AUS500' from dual union all
select 'Jan-20' from dual union all
select 'Four200' from dual union all
select null from dual union all
select '200abc' from dual union all
select '3' from dual
)
select str, replace(str, rtrim(str, '0123456789')) as last_digits
from sample_data
;
STR LAST_DIGITS
--------- -----------
100AUS50 50
100AUS100 100
100AUS500 500
Jan-20 20
Four200 200
200abc
3 3
以前我只会说"standard string functions are generally faster than regular expressions"。不过好在前几天刚在OTN上回答过同样的问题,楼主问的是性能对比。在包含 150 万个输入字符串的示例中,我刚刚在此处展示的解决方案快了五倍(0.86 秒,而正则表达式解决方案为 4.4 秒)。 https://community.oracle.com/message/15598613#15598613
我想使用 regexp_substr 到 return 字符串值
中字符后的数字例如。
字符串值 return
100AUS50 50
100澳元100 100
100AUS500 500
1-20 20
四 200 200
您可以使用以下方法获取号码
select regexp_substr(column,'\d+$') from TABLE;
这是使用专门的标准字符串函数(无正则表达式)执行此操作的一种方法。 rtrim
函数只是 删除 输入字符串末尾的数字。然后 replace
从原始字符串中替换该字符串(输入字符串 减去 末尾的数字)——因此在输出中留下 "digits at the end"。
with
sample_data (str) as (
select '100AUS50' from dual union all
select '100AUS100' from dual union all
select '100AUS500' from dual union all
select 'Jan-20' from dual union all
select 'Four200' from dual union all
select null from dual union all
select '200abc' from dual union all
select '3' from dual
)
select str, replace(str, rtrim(str, '0123456789')) as last_digits
from sample_data
;
STR LAST_DIGITS
--------- -----------
100AUS50 50
100AUS100 100
100AUS500 500
Jan-20 20
Four200 200
200abc
3 3
以前我只会说"standard string functions are generally faster than regular expressions"。不过好在前几天刚在OTN上回答过同样的问题,楼主问的是性能对比。在包含 150 万个输入字符串的示例中,我刚刚在此处展示的解决方案快了五倍(0.86 秒,而正则表达式解决方案为 4.4 秒)。 https://community.oracle.com/message/15598613#15598613