如何搜索特殊字符“|”后的所有数字使用甲骨文
How to search for the All the numbers after special character "|" using Oracle
基本上我有很少的记录,在具有特殊字符的字段中通常如下所示:
id Content
1 1|1232
2 23|12323
3 33|233223
我想编写一个查询,select 管道“|”右侧的数字
所以结果应该是这样的:
查询结果
1 = 1232
2 = 12323
分机...
您可以使用简单的字符串函数:
SELECT id,
SUBSTR( content, 1, INSTR( content, '|' ) - 1 ) AS before_pipe,
SUBSTR( content, INSTR( content, '|' ) + 1 ) AS after_pipe
FROM table_name
或者,使用正则表达式:
SELECT id,
REGEXP_SUBSTR( content, '^\d+' ) AS before_pipe,
REGEXP_SUBSTR( content, '\d+$' ) AS after_pipe
FROM table_name
其中,对于示例数据:
CREATE TABLE table_name ( id, content ) AS
SELECT 1, '1|1232' FROM DUAL UNION ALL
SELECT 2, '23|12323' FROM DUAL UNION ALL
SELECT 3, '33|233223' FROM DUAL;
双输出:
ID
BEFORE_PIPE
AFTER_PIPE
1
1
1232
2
23
12323
3
33
233223
db<>fiddle here
根据示例数据,查看这两个选项(substr + instr 和正则表达式)中的任何一个是否有帮助。第 1 - 5 行中的示例数据,查询从第 6 行开始。
SQL> with test (id, content) as
2 (select 1, '1|1232' from dual union all
3 select 2, '23|12323' from dual union all
4 select 3, '33|233223' from dual
5 )
6 select id,
7 content,
8 --
9 substr(content, instr(content, '|') + 1) result_1,
10 regexp_substr(content, '\d+$') result_2
11 from test;
ID CONTENT RESULT_1 RESULT_2
---------- --------- --------------- ---------------
1 1|1232 1232 1232
2 23|12323 12323 12323
3 33|233223 233223 233223
SQL>
基本上我有很少的记录,在具有特殊字符的字段中通常如下所示:
id Content
1 1|1232
2 23|12323
3 33|233223
我想编写一个查询,select 管道“|”右侧的数字
所以结果应该是这样的: 查询结果
1 = 1232
2 = 12323
分机...
您可以使用简单的字符串函数:
SELECT id,
SUBSTR( content, 1, INSTR( content, '|' ) - 1 ) AS before_pipe,
SUBSTR( content, INSTR( content, '|' ) + 1 ) AS after_pipe
FROM table_name
或者,使用正则表达式:
SELECT id,
REGEXP_SUBSTR( content, '^\d+' ) AS before_pipe,
REGEXP_SUBSTR( content, '\d+$' ) AS after_pipe
FROM table_name
其中,对于示例数据:
CREATE TABLE table_name ( id, content ) AS
SELECT 1, '1|1232' FROM DUAL UNION ALL
SELECT 2, '23|12323' FROM DUAL UNION ALL
SELECT 3, '33|233223' FROM DUAL;
双输出:
ID BEFORE_PIPE AFTER_PIPE 1 1 1232 2 23 12323 3 33 233223
db<>fiddle here
根据示例数据,查看这两个选项(substr + instr 和正则表达式)中的任何一个是否有帮助。第 1 - 5 行中的示例数据,查询从第 6 行开始。
SQL> with test (id, content) as
2 (select 1, '1|1232' from dual union all
3 select 2, '23|12323' from dual union all
4 select 3, '33|233223' from dual
5 )
6 select id,
7 content,
8 --
9 substr(content, instr(content, '|') + 1) result_1,
10 regexp_substr(content, '\d+$') result_2
11 from test;
ID CONTENT RESULT_1 RESULT_2
---------- --------- --------------- ---------------
1 1|1232 1232 1232
2 23|12323 12323 12323
3 33|233223 233223 233223
SQL>