原始数据是:
xy12j-ty75#pid
bvbn-8ffde#pid
gonw-5gt#pid
qertt-yytre#pid
我只需要显示" - "one_answers"#"之间的数据:
ty75
8ffde
5gt
yytre
我编写了2种不同的代码,如果将(某种程度上(组合在一起,我认为会起作用。
我的代码1(显示"#"左侧的所有内容:
SQL> select substr(pcode, 1, (instr (pcode, '#'))-1)
2 from products ;
显示以下内容:
----------------------------------------
xy12j-ty75
bvbn-8ffde
vb-5gt
skr-yytre
和我的代码2显示了" - "右侧的所有内容:
SQL> select ltrim(pcode, (substr(pcode, 1, (instr(pcode, '-')))))
2 from products ;
显示以下内容:
----------------------------------------
ttg#pid
lbcx#pid
gonw#pid
ertt#pid
为此我使用subsr和inster函数也很重要。
您可以使用正则表达式
select regexp_substr(pcode, '(.*?-)(.*?)#',1,1,'',2) from products
,例如
with products (pcode) as (
select 'xy12j-ty75#pid' from dual union all
select 'bvbn-8ffde#pid' from dual union all
select 'gonw-5gt#pid' from dual union all
select 'qertt-yytre#pid' from dual
)
select regexp_substr(pcode, '(.*?-)(.*?)#',1,1,'',2) from products
假设您的字符串始终与示例数据的模式匹配,则可以通过这种方式组合instr
和substr
:
select substr(pCode,
instr(pCode, '-') + 1,
instr(pCode, '#') - instr(pCode, '-') -1
)
from products
例如,这个
with products(pCode) as (
select 'xy12j-ty75#pid' from dual union all
select 'bvbn-8ffde#pid' from dual union all
select 'gonw-5gt#pid' from dual union all
select 'qertt-yytre#pid' from dual
)
select substr(pCode, instr(pCode, '-')+1, instr(pCode, '#') - instr(pCode, '-')-1)
from products
给出:
ty75
8ffde
5gt
yytre
在这里,您使用instr
在'-'
之后从CHAR开始,以及instr
和substr
的组合来计算'-'
和'#'
之间的字符数。
您可以使用replace
使用instr
&substr
字符串操作:
select replace( substr(pcode, 1, (instr (pcode, '#'))-1),
substr(pcode, 1, (instr (pcode, '-'))) ) "Result Set"
from products;
Result Set
----------
ty75
8ffde
5gt
yytre
demo