Oracle 中 sql 中的 Substr 给出不正确的输出
Substr giving incorrect output in sql in oracle
我有 table ot.plan:
其中有
NAME
--------------------------------------------------------------------------------
EXISTS(SELECT 1 from zzz_temp b where SUBSTR(a.hehrircal,1,4)=b.acc_num
EXISTS(SELECT 1 from zzz_temp b where SUBSTR(a.clinet,1,4)=b.acc_num
我的预期输出是:
NAME
--------------------------------------------------------------------------------
zzz_temp
zzz_temp
我试过了:
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,'b')) from ot.plan;
它给我输出:
zzz_temp b where SUBSTR(a.hehri
zzz_temp b where SUBSTR(a.cline
我该如何解决?
你好像在想最后一个参数是停止的位置。它实际上是要包含在结果中的字符数。
尝试用结束位置减去开始位置来确定长度。
您可以使用 regexp_substr
实现所需的结果,如下所示:
Select regexp_substr(name, 'from (.+) b',1,1,null,1)
From ot.plan;
干杯!!
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,' b')-INSTR(name,'from')+5) from ot.plan; still not working
您正试图从一个数字中减去另一个数字,但作为其中的一部分,您有一个加法。您要么需要将第二部分括在括号中:
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,' b')-(INSTR(name,'from')+5)) from ot.plan;
SUBSTR(NAME,INSTR(NAME,'FROM')+5,INSTR(NAME,'B')-(INSTR(NAME,'FROM')+5)
-----------------------------------------------------------------------
zzz_temp
zzz_temp
或将加号改为减号:
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,' b')-INSTR(name,'from')-5) from ot.plan;
SUBSTR(NAME,INSTR(NAME,'FROM')+5,INSTR(NAME,'B')-INSTR(NAME,'FROM')-5)
-----------------------------------------------------------------------
zzz_temp
zzz_temp
您可以查看涉及的各个数字以了解其作用:
select
INSTR(name,'from') as start_from,
INSTR(name,'from')+5 as start_table,
INSTR(name,' b') as b,
INSTR(name,' b')-(INSTR(name,'from')+5) as length_a,
INSTR(name,' b')-INSTR(name,'from')-5 as length_b
from ot.plan;
START_FROM START_TABLE B LENGTH_A LENGTH_B
---------- ----------- ---------- ---------- ----------
17 22 30 8 8
17 22 30 8 8
前三位是您要查找的字符的位置;长度是减法的两个变体 - 所以整体显示您正在寻找从位置 22 开始的 8 个字符。
但是,考虑到评论中提到的这种方式的脆弱性,无论如何,您最好还是使用正则表达式方法。
我有 table ot.plan:
其中有
NAME
--------------------------------------------------------------------------------
EXISTS(SELECT 1 from zzz_temp b where SUBSTR(a.hehrircal,1,4)=b.acc_num
EXISTS(SELECT 1 from zzz_temp b where SUBSTR(a.clinet,1,4)=b.acc_num
我的预期输出是:
NAME
--------------------------------------------------------------------------------
zzz_temp
zzz_temp
我试过了:
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,'b')) from ot.plan;
它给我输出:
zzz_temp b where SUBSTR(a.hehri
zzz_temp b where SUBSTR(a.cline
我该如何解决?
你好像在想最后一个参数是停止的位置。它实际上是要包含在结果中的字符数。
尝试用结束位置减去开始位置来确定长度。
您可以使用 regexp_substr
实现所需的结果,如下所示:
Select regexp_substr(name, 'from (.+) b',1,1,null,1)
From ot.plan;
干杯!!
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,' b')-INSTR(name,'from')+5) from ot.plan; still not working
您正试图从一个数字中减去另一个数字,但作为其中的一部分,您有一个加法。您要么需要将第二部分括在括号中:
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,' b')-(INSTR(name,'from')+5)) from ot.plan;
SUBSTR(NAME,INSTR(NAME,'FROM')+5,INSTR(NAME,'B')-(INSTR(NAME,'FROM')+5)
-----------------------------------------------------------------------
zzz_temp
zzz_temp
或将加号改为减号:
select SUBSTR(name,INSTR(name,'from')+5,INSTR(name,' b')-INSTR(name,'from')-5) from ot.plan;
SUBSTR(NAME,INSTR(NAME,'FROM')+5,INSTR(NAME,'B')-INSTR(NAME,'FROM')-5)
-----------------------------------------------------------------------
zzz_temp
zzz_temp
您可以查看涉及的各个数字以了解其作用:
select
INSTR(name,'from') as start_from,
INSTR(name,'from')+5 as start_table,
INSTR(name,' b') as b,
INSTR(name,' b')-(INSTR(name,'from')+5) as length_a,
INSTR(name,' b')-INSTR(name,'from')-5 as length_b
from ot.plan;
START_FROM START_TABLE B LENGTH_A LENGTH_B
---------- ----------- ---------- ---------- ----------
17 22 30 8 8
17 22 30 8 8
前三位是您要查找的字符的位置;长度是减法的两个变体 - 所以整体显示您正在寻找从位置 22 开始的 8 个字符。
但是,考虑到评论中提到的这种方式的脆弱性,无论如何,您最好还是使用正则表达式方法。