从 Oracle 10g 中的数据集中检索特定数字
Retrieve certain number from data set in Oracle 10g
1. <0,0><120.96,2000><241.92,4000><362.88,INF>
2. <0,0><143.64,2000><241.92,4000><362.88,INF>
3. <0,0><125.5,2000><241.92,4000><362.88,INF>
4. <0,0><127.5,2000><241.92,4000><362.88,INF>
以上是我在Oracle 10g中的数据集。我需要如下输出
1. 120.96
2. 143.64
3. 125.5
4. 125.5
我想要的输出只是在"comma" (120.96)
之前。我尝试使用 REGEXP_SUBSTR 但无法获得任何输出。如果有人能提供有效的方法来解决这个问题,那将非常有帮助
这里有一个方法,首先解析出第二个元素,然后获取其中的第一个数字:
select regexp_substr(regexp_substr(x, '<[^>]*>', 1, 2), '[0-9.]+', 1, 1)
另一种方法只是获取字符串中的第三个数字:
select regexp_substr(x, '[0-9.]+', 1, 3)
这是一种不使用 Regexp 的方法。
查找第二次出现“<”的索引。然后找到第二次出现的 ',' 在子字符串中使用这些值。
with
data as
(
select '<0,0><120.96,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><143.64,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual
)
select substr(x, instr(x,'<',1,2)+1, instr(x,',',1,2)- instr(x,'<',1,2)-1)
from data
使用正则表达式的方法:
识别第 2 次出现的数值后跟逗号
然后删除结尾的逗号。
with
data as
(
select '<0,0><120.96,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><143.64,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual
)
select
trim(TRAILING ',' FROM regexp_substr(x,'[0-9.]+,',1,2))
from data
此示例使用 regexp_substr 获取包含在第二次出现的小于号和逗号中的字符串:
SQL> with tbl(id, str) as (
select 1, '<0,0><120.96,2000><241.92,4000><362.88,INF>' from dual union
select 2, '<0,0><143.64,2000><241.92,4000><362.88,INF>' from dual union
select 3, '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual union
select 4, '<0,0><127.5,2000><241.92,4000><362.88,INF>' from dual
)
select id,
regexp_substr(str, '<(.*?),', 1, 2, null, 1) value
from tbl;
ID VALUE
---------- -------------------------------------------
1 120.96
2 143.64
3 125.5
4 127.5
编辑:我意识到 OP 指定了 10g,而我给出的 regexp_substr 示例使用了在 11g 中添加的第 6 个参数(子组)。这是一个使用 regexp_replace 的示例,它应该适用于 10g:
SQL> with tbl(id, str) as (
select 1, '<0,0><120.96,2000><241.92,4000><362.88,INF>' from dual union
select 2, '<0,0><143.64,2000><241.92,4000><362.88,INF>' from dual union
select 3, '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual union
select 4, '<0,0><127.5,2000><241.92,4000><362.88,INF>' from dual
)
select id,
regexp_replace(str, '^(.*?)><(.*?),.*$', '') value
from tbl;
ID VALUE
---------- ----------
1 120.96
2 143.64
3 125.5
4 127.5
SQL>
1. <0,0><120.96,2000><241.92,4000><362.88,INF>
2. <0,0><143.64,2000><241.92,4000><362.88,INF>
3. <0,0><125.5,2000><241.92,4000><362.88,INF>
4. <0,0><127.5,2000><241.92,4000><362.88,INF>
以上是我在Oracle 10g中的数据集。我需要如下输出
1. 120.96
2. 143.64
3. 125.5
4. 125.5
我想要的输出只是在"comma" (120.96)
之前。我尝试使用 REGEXP_SUBSTR 但无法获得任何输出。如果有人能提供有效的方法来解决这个问题,那将非常有帮助
这里有一个方法,首先解析出第二个元素,然后获取其中的第一个数字:
select regexp_substr(regexp_substr(x, '<[^>]*>', 1, 2), '[0-9.]+', 1, 1)
另一种方法只是获取字符串中的第三个数字:
select regexp_substr(x, '[0-9.]+', 1, 3)
这是一种不使用 Regexp 的方法。 查找第二次出现“<”的索引。然后找到第二次出现的 ',' 在子字符串中使用这些值。
with
data as
(
select '<0,0><120.96,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><143.64,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual
)
select substr(x, instr(x,'<',1,2)+1, instr(x,',',1,2)- instr(x,'<',1,2)-1)
from data
使用正则表达式的方法: 识别第 2 次出现的数值后跟逗号 然后删除结尾的逗号。
with
data as
(
select '<0,0><120.96,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><143.64,2000><241.92,4000><362.88,INF>' x from dual
UNION ALL
select '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual
)
select
trim(TRAILING ',' FROM regexp_substr(x,'[0-9.]+,',1,2))
from data
此示例使用 regexp_substr 获取包含在第二次出现的小于号和逗号中的字符串:
SQL> with tbl(id, str) as (
select 1, '<0,0><120.96,2000><241.92,4000><362.88,INF>' from dual union
select 2, '<0,0><143.64,2000><241.92,4000><362.88,INF>' from dual union
select 3, '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual union
select 4, '<0,0><127.5,2000><241.92,4000><362.88,INF>' from dual
)
select id,
regexp_substr(str, '<(.*?),', 1, 2, null, 1) value
from tbl;
ID VALUE
---------- -------------------------------------------
1 120.96
2 143.64
3 125.5
4 127.5
编辑:我意识到 OP 指定了 10g,而我给出的 regexp_substr 示例使用了在 11g 中添加的第 6 个参数(子组)。这是一个使用 regexp_replace 的示例,它应该适用于 10g:
SQL> with tbl(id, str) as (
select 1, '<0,0><120.96,2000><241.92,4000><362.88,INF>' from dual union
select 2, '<0,0><143.64,2000><241.92,4000><362.88,INF>' from dual union
select 3, '<0,0><125.5,2000><241.92,4000><362.88,INF>' from dual union
select 4, '<0,0><127.5,2000><241.92,4000><362.88,INF>' from dual
)
select id,
regexp_replace(str, '^(.*?)><(.*?),.*$', '') value
from tbl;
ID VALUE
---------- ----------
1 120.96
2 143.64
3 125.5
4 127.5
SQL>