首页 > 解决方案 > ORACLE - 从字符串中选择数字的表达式

问题描述

我有这张表,我需要选择字母“kV”或“KV”之前的数字。同样在最后一个示例中,el 编号在开头没有空格。

这里的例子:

select device_name from table;

--device_name can be:

15518 - 132 Garden Plain 138 kV l/o 0405 result 138
ALLENIM 345 KV  ALL-RPM_I.  result=345
179 BLOO345 KV  TR84CT-P_I.  result= 345.  --on this case the number is withoutspace 

我可以使用什么样的正则表达式来选择这些数字?

问候

标签: sqlregexoracle

解决方案


这是一种选择:在 upper 前面找到数字KV,然后删除KV

SQL> with test (col) as
  2    (select '15518 - 132 Garden Plain 138 kV l/o 0405' from dual union all-- result 138
  3     select 'ALLENIM 345 KV  ALL-RPM_I.'               from dual union all-- result=345
  4     select '179 BLOO345 KV  TR84CT-P_I.'              from dual          -- result=345
  5    )
  6  select col,
  7    replace(regexp_substr(upper(col), '\d+(\s?)KV'), 'KV', '') result
  8  from test;

COL                                      RESULT
---------------------------------------- --------------------
15518 - 132 Garden Plain 138 kV l/o 0405 138
ALLENIM 345 KV  ALL-RPM_I.               345
179 BLOO345 KV  TR84CT-P_I.              345

SQL>

推荐阅读