【问题标题】:how to parse first and second occurrence of number between two strings如何解析两个字符串之间第一次和第二次出现的数字
【发布时间】:2020-01-30 22:47:23
【问题描述】:

你能帮我解析字符串中的数字吗?

我想从字符串中选择第一次和第二次出现的数字:

 PKGGeneral.SetObjectAttribute(i_DimObject=4,i_ObjectID=163225122,i_Attribute=NAME,i_Value (VarChar2)=xDSL:1.4.51);

PKGPort.CreateLogicalPort(io_portid=197604073,i_name=VLAN_segment:7239554:IPTV GPON_Port_A,i_nodeid=123431890,i_porttypeid=1900000150,i_bandwidthid=1,i_parentportid=186300246);

第一次出现在 A 列中,第二次出现在 B 列中。

结果: 示例 A

column A : 4
column B : 163225122

示例 B

column A : 197604073
column B : 7239554

谢谢。

【问题讨论】:

  • Edit 问题并提供您到目前为止尝试过的内容以及您面临的问题。

标签: sql regex oracle substring instr


【解决方案1】:

使用regexp_substr():

select
    regexp_substr(mycol, '\d+', 1, 1) colA,
    regexp_substr(mycol, '\d+', 1, 2) colB
from mytable

\d+ 捕获一系列连续的数字。第四个参数指示应该返回哪个事件。

Demo on DB Fiddle

with mytable as (
    select 'PKGGeneral.SetObjectAttribute(i_DimObject=4,i_ObjectID=163225122,i_Attribute=NAME,i_Value (VarChar2)=xDSL:1.4.51);' mycol from dual
    union all
    select 'PKGPort.CreateLogicalPort(io_portid=197604073,i_name=VLAN_segment:7239554:IPTV GPON_Port_A,i_nodeid=123431890,i_porttypeid=1900000150,i_bandwidthid=1,i_parentportid=186300246);' from dual
)
select
    regexp_substr(mycol, '\d+', 1, 1) colA,
    regexp_substr(mycol, '\d+', 1, 2) colB
from mytable
可乐 | COLB :-------- | :-------- 4 | 163225122 197604073 | 7239554

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2018-02-04
    相关资源
    最近更新 更多