您的位置:首页 > 数据库 > Oracle

Oracle截取字符串函数function

2018-09-12 13:35 295 查看

1.

CREATE OR REPLACE FUNCTION split (p_list CLOB, p_sep VARCHAR2 := ',')
RETURN tabletype
PIPELINED
/**************************************
* Name: split
* Author: Zyj.
* Date: 2016-12-03.
* Function: 返回字符串被指定字符分割后的表类型。
* Parameters: p_list: 待分割的字符串。
p_sep: 分隔符,默认逗号,也可以指定字符或字符串。
* Example: SELECT *
FROM users
WHERE u_id IN (SELECT COLUMN_VALUE
FROM table (split ('1,2')))
返回u_id为1和2的两行数据。
**************************************/
IS
l_idx PLS_INTEGER;
v_list VARCHAR2 (4000) := p_list;
BEGIN
LOOP
l_idx := INSTR (v_list, p_sep);
IF l_idx > 0
THEN
PIPE ROW (SUBSTR (v_list, 1, l_idx - 1));
v_list := SUBSTR (v_list, l_idx + LENGTH (p_sep));
ELSE
PIPE ROW (v_list);
EXIT;
END IF;
END LOOP;
END;

2.

CREATE OR REPLACE TYPE "TABLETYPE" as table of VARCHAR2(4000);

3.

create or replace function splitStr(str in clob,
i  in number := 0,
sep in varchar2 := ',') return varchar2 is
t_i    number;
t_count number;
t_str  varchar2(4000);
begin
if i = 0 then
t_str := str;
elsif instr(str, sep) = 0 then
t_str := sep;
else
select count(*) into t_count from table(split(str, sep));

if i <= t_count then
select str
into t_str
from (select rownum as item, column_value as str
from table(split(str, sep)))
where item = i;
end if;
end if;

return t_str;
end /**
@author ZYJ 2016-06-12 截取字符串
ex:splitStr('2016-05',1,'-') return 2016;
*/;
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: