字符串分割函数(用途:答题流水解析等)

问题:向数据库传入答题流水字符串'101|A,102|B,103|D',如何写到答题明细表中?

思路:首先要对字符串分解

SELECT column_value FROM TABLE(F_SPLITSTR('101|A,102|B,103|D',','));

--输出

101|A
102|B
103|D;

然后对每个问题解析:

select F_GET_STRSPLIT('102|B','|','1') from dual;

--输出

102

select F_GET_STRSPLIT('102|B','|','2') from dual;

--输出

B

总结:附上两个函数的代码

--切割函数

CREATE OR REPLACE FUNCTION "F_SPLITSTR" (ids IN VARCHAR2,sep IN VARCHAR2 := ',')
RETURN tpbs_str_table IS
str_table tpbs_str_table := tpbs_str_table();
v BINARY_INTEGER;
a_pos INTEGER;
tmp VARCHAR2(32767);
BEGIN
tmp := ids;
IF length(tmp) > 0 THEN
a_pos := instr(tmp, sep);
WHILE a_pos > 0 LOOP
str_table.EXTEND;
v := str_table.COUNT;
str_table(v) := substr(tmp, 1, a_pos - 1);
tmp := substr(tmp, a_pos + 1);
a_pos := instr(tmp, sep);
END LOOP;
str_table.EXTEND;
v := str_table.COUNT;
str_table(v) := (tmp);
END IF;

RETURN str_table;
END;

--取值函数

CREATE OR REPLACE FUNCTION "F_GET_STRSPLIT" (
p_str varchar2, --要分割的字符串
p_split varchar2, --分隔符号
p_index int --取第几个元素
)
return varchar2
-- select f_get_strsplit(202415, 'CS2021', '2') from dual;
as
t_location int;
t_start int;
t_next int;
t_seed int;
t_p_str varchar2(2000);
v_result varchar2(1024);


begin

t_p_str := ltrim(rtrim(p_str));
t_start := 1 ;
t_next := 1 ;
t_seed := length(p_split);
t_location := instr(t_p_str,p_split);

while t_location<>0 and p_index > t_next loop
t_start := t_location+t_seed;
t_location := instr(t_p_str,p_split,t_start);
t_next := t_next+1;
end loop;

if t_location =0 then
t_location :=length(t_p_str)+1;
end if;
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,t_location为0,

--那默认为字符串后边有一个分隔符号。

v_result := substr(t_p_str,t_start,t_location-t_start);

if v_result is null then
v_result := '';
end if;

return v_result;
end f_get_strsplit;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24156512/viewspace-719057/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24156512/viewspace-719057/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值