ORACLE 怎么拆分字符串
拆分sql后:
select
substr('Hello World',0,instr('Hello World','o')) as 首,
substr('Hello World',instr('Hello World','o')+1) as 尾
from dual;
有什么规定吗?
“ABCDEFGH”或是“A,B,C,D,E,F,G,H"
按符号拆分?按位置拆分?
substr()函数:截取a-b之间的或a 。如:substr('Hello World',0,5)截取1-5或
substr('Hello World',0,instr('Hello World','o'))截取1-"o"的位置
1.如果是","等符号:
select substr('Hello World',0,instr('Hello World','o')) from dual;
select substr('Hello World',instr('Hello World','o')+1) from dual;
或
select substr('Hello World',instr('Hello World','o')+1,length('Hello World')) from dual;
推荐于2016-03-13 · 知道合伙人软件行家
CREATE OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000);
CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN str_split
PIPELINED
AS
v_length NUMBER := LENGTH(p_string);
v_start NUMBER := 1;
v_index NUMBER;
BEGIN
WHILE(v_start <= v_length)
LOOP
v_index := INSTR(p_string, p_delimiter, v_start);
IF v_index = 0
THEN
PIPE ROW(SUBSTR(p_string, v_start));
v_start := v_length + 1;
ELSE
PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start));
v_start := v_index + 1;
END IF;
END LOOP;
RETURN;
END splitstr;
创建完毕后,我们来测试一下,例如执行如下SQL:
select * from table(splitstr('Hello,Cnblogs!',','));