输入:
start1
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
start2
我想捕获start1和start2之间的任何字符串。它们之间的字符串是可变的。 输出:
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
使用子字符串/索引功能
答案 0 :(得分:0)
首先,必须使用订单。根据SQL规则,如果您不使用order by,那么数据库可以以任何顺序返回结果,甚至与上次执行相同查询时的结果不同。
假设您按ID执行订单,并且您要查询的字段名称是名称
select name
from table1
where id > (select id from table1 where name = 'start1')
and id < (select id from table1 where name = 'start2')
order by id
答案 1 :(得分:0)
我猜这里是因为无法理解所需要的东西:
SELECT TRIM(SUBSTR(str, start1+1, btwn_starts)) final_str
FROM
(
SELECT 'start1 '||' some data in between '||' start2' str
, Length('start1') start1
, Length('start2') start2
, Length(' some data in between ') btwn_starts
FROM dual
)
/
Output string: some data in between
答案 2 :(得分:0)
如果它在pl / sql中,那么你可以这样做:
SQL> declare
2 v_str varchar2(2000) := 'start1
3 david@gmail.com
4 david@gmail.com
5 david@gmail.com
6 david@gmail.com
7 david@gmail.com
8 david@gmail.com
9 david@gmail.com
10 start2';
11 v_newstr varchar2(2000);
12 v_start_delim varchar2(10) := 'start1';
13 v_end_delim varchar2(10) := 'start2';
14 begin
15 v_newstr := substr(v_str, instr(v_str, v_start_delim)+length(v_start_delim),
16 instr(v_str, v_end_delim) - instr(v_str, v_start_delim) - length(v_start_delim));
17
18 -- remove leading/trailing breaks.
19 v_newstr := trim(both chr(10) from v_newstr);
20 dbms_output.put_line(v_newstr);
21
22 end;
23 /
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
PL/SQL procedure successfully completed.
或者如果你在SQL中有字符串(假设字符串全部在一行中)
SQL> select trim(both chr(10) from
2 substr(str, instr(str, 'start1')+length('start1'),
3 instr(str, 'start2') - instr(str, 'start1') - length('start1'))
4 ) newstr
5 from data;
NEWSTR
-----------------------
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com
david@gmail.com