oracle 从包含字符串的列中提取数字

bt1cpqcv  于 2022-11-03  发布在  Oracle
关注(0)|答案(3)|浏览(346)

所以我有一个函数,它返回字符串的组合(多个值)。我需要提取后面跟有char“DL:“的所有内容。但仅限于此。
因此在提取之前:


**pck_import.GETdocnumber(XML_DATA)**

________________________________________
DL:2212200090001 Pr:8222046017
________________________________________
Obj:020220215541 DL:1099089729
________________________________________
DL:DST22017260
________________________________________
DL:22122000123964 Pr:8222062485
________________________________________
DL:22122000108599
________________________________________
Obj:0202200015539 DL:2100001688

在每种情况下,我都需要在char“DL:“后面加上“number”。“DL:“可以是单独的,可以是第一个位置(在多个值之间),也可以是最后一个字符串。而且在某些情况下,“DL:“值也包含char。
因此,输出:


**OUTPUT**

______________
2212200090001
______________
1099089729
______________
DST22017260
______________
22122000123964
______________
22122000108599
______________
2100001688

我试探着:

substr(pck_import.GETdocnumber(XML_DATA),
instr(pck_import.GETdocnumber(XML_DATA),
'DL:') + 3))

这也会传回“Pr:“。

sbtkgmzw

sbtkgmzw1#

with s as (
select 'DL:2212200090001 Pr:8222046017'  str from dual union all
select 'Obj:020220215541 DL:1099089729'  str from dual union all
select 'DL:DST22017260'                  str from dual union all
select 'DL:22122000123964 Pr:8222062485' str from dual union all
select 'DL:22122000108599'               str from dual union all
select 'Obj:0202200015539 DL:2100001688' str from dual)
select str, regexp_substr(str, 'DL:(\S+)', 1, 1, null, 1) rs
from s;

STR                             RS
------------------------------- -------------------------------
DL:2212200090001 Pr:8222046017  2212200090001
Obj:020220215541 DL:1099089729  1099089729
DL:DST22017260                  DST22017260
DL:22122000123964 Pr:8222062485 22122000123964
DL:22122000108599               22122000108599
Obj:0202200015539 DL:2100001688 2100001688

6 rows selected
5sxhfpxr

5sxhfpxr2#

像这样的东西?
示例数据:

SQL> with test (col) as
  2    (select
  3  '________________________________________
  4  DL:2212200090001 Pr:8222046017
  5  ________________________________________
  6  Obj:020220215541 DL:1099089729
  7  ________________________________________
  8  DL:DST22017260
  9  ________________________________________
 10  DL:22122000123964 Pr:8222062485
 11  ________________________________________
 12  DL:22122000108599
 13  ________________________________________
 14  Obj:0202200015539 DL:2100001688'
 15  from dual)
 16  --

查询:

17  select replace(regexp_substr(col, 'DL:\w+', 1, level), 'DL:') result
 18  from test
 19  connect by level <= regexp_count(col, 'DL:');

RESULT
--------------------------------------------------------------------------------
2212200090001
1099089729
DST22017260
22122000123964
22122000108599
2100001688

6 rows selected.

SQL>

(note如果您要处理的数据不止一行,则可能需要修改该查询)

np8igboo

np8igboo3#

You could achieve this by using regular expressions utilising a positive lookbehind and lookahead.
The regex (?<=DL\:)\d*(?=\s)' matches all digits between DL: until a single whitespace character occurs.
You'd want to use the REGEXP_SUBSTR function for this (as you tagged this question with OracleSQL):

SELECT
  REGEXP_SUBSTR(my_column,
                '(?<=DL\:)\d*(?=\s)') "DL field"
  FROM my_table;

If you want to match substrings like DST22017260 as well, using . (any character) instead of \d would work: (?<=DL\:).*(?=\s) .

相关问题