使用Oracle SQL中的regexp_substr从字符串中提取特定关键字

huangapple go评论64阅读模式
英文:

Extract a particular keyword from string using regexp_substr in Oracle SQL

问题

I want to extract the code next to AC that is 10035.
我想提取AC后面的代码,即10035。

It does give the code but it is not necessary in some of the strings that the code is present at the 5 position.
它确实提供了代码,但并不是在所有字符串中都需要,因为代码可能出现在第5位置。

Like in this string Phone_up_gsk_pdp_AC_90054 it is present in the 6 position.
就像在这个字符串Phone_up_gsk_pdp_AC_90054中,代码出现在第6位置一样。

Is there a way to use the reg_exp to get only the digits after matching AC. The code will always be of 5 digits.
是否有办法使用正则表达式仅获取匹配AC后面的数字。代码始终是5位数字。

英文:

I have several strings in column Phone_mid_bsnl_AC_10035 and I want to extract the code next to AC that is 10035.

I used this SQL code:

WITH CTE(STR) AS 
(
    SELECT 'Phone_mid_bsnl_AC_10035' 
    FROM DUAL
)
SELECT REGEXP_SUBSTR(STR, '[^_]+', 1, 5) 
FROM CTE

It does give the code but it is not necessary in some of the strings that the code is present at the 5 position. Like in this string Phone_up_gsk_pdp_AC_90054 it is present in the 6 position. Is there a way to use the reg_exp to get only the digits after matching AC. The code will always be of 5 digits.

答案1

得分: 1

使用以下代码替代:

SELECT REGEXP_SUBSTR(STR, '_(\d{5})(_|$)', 1, 1, null, 1) FROM CTE

Demo here.

WITH CTE(STR) AS (
SELECT 'Phone_mid_bsnl_A_C_10035' FROM DUAL union all
  SELECT 'Phone_mid_bsnl_AC_10035' FROM DUAL union all
  SELECT 'Phone_up_gsk_pdp_AC_90054' FROM DUAL union all
  SELECT 'Phone_mid_bsnl_A_C_10035_sad' FROM DUAL union all
  SELECT 'Phone_mid_bsnl_A_C_1003' FROM DUAL 
)
SELECT REGEXP_SUBSTR(STR, '_(\d{5})(_|$)', 1, 1, null, 1) a FROM CTE
a
10035
10035
90054
10035
null
英文:

Use

SELECT REGEXP_SUBSTR(STR, '_(\d{5})(_|$)', 1, 1, null, 1) FROM CTE

instead. It will extract first occurence of 5 digits between _ and _ or end of the string.

Demo here.

WITH CTE(STR) AS (
SELECT 'Phone_mid_bsnl_A_C_10035' FROM DUAL union all
  SELECT 'Phone_mid_bsnl_AC_10035' FROM DUAL union all
  SELECT 'Phone_up_gsk_pdp_AC_90054' FROM DUAL union all
  SELECT 'Phone_mid_bsnl_A_C_10035_sad' FROM DUAL union all
  SELECT 'Phone_mid_bsnl_A_C_1003' FROM DUAL 
)
SELECT REGEXP_SUBSTR(STR, '_(\d{5})(_|$)', 1, 1, null, 1) a FROM CTE
a
10035
10035
90054
10035
null

答案2

得分: 1

也可以使用 regexp_replace

WITH CTE(STR) AS (
SELECT 'Phone_mid_bsnl_AC_10035' FROM DUAL union all
SELECT 'Phone_AC_10036_something' FROM DUAL
)
SELECT regexp_replace(STR, '.*AC_([0-9]{5}).*','') FROM CTE

示例在这里

英文:

It also can be done using regexp_replace

WITH CTE(STR) AS (
SELECT 'Phone_mid_bsnl_AC_10035' FROM DUAL union all
SELECT 'Phone_AC_10036_something' FROM DUAL
)
SELECT regexp_replace(STR, '.*AC_([0-9]{5}).*','') FROM CTE

Demo here

答案3

得分: 0

如果数字值始终位于字符串末尾,则可以简化为

示例数据:

SQL> with test (col) as
  2    (select 'Phone_mid_bsnl_AC_10035' from dual)

查询:

  3  select substr(col, instr(col, '_', -1) + 1) result_1,
  4         regexp_substr(col, '\d+$') result_2
  5  from test;

RESULT_1   RESULT_2
---------- ----------
10035      10035

SQL>
  • result_1: 返回跟在最后一个下划线字符后的字符串
  • result_2: 返回数值字符串,与源字符串末尾“锚定”
英文:

If numeric value is always at the end of a string, then you could simplify it to

sample data:

SQL> with test (col) as
  2    (select 'Phone_mid_bsnl_AC_10035' from dual)

Query:

  3  select substr(col, instr(col, '_', -1) + 1) result_1,
  4         regexp_substr(col, '\d+$') result_2
  5  from test;

RESULT_1   RESULT_2
---------- ----------
10035      10035

SQL>
  • result_1: returns string that follows the last underline character
  • result_2: returns numeric string "anchored" to the end of the source string

huangapple
  • 本文由 发表于 2023年4月19日 18:06:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定