PLSQL实现函数取varchar中的数字
近期使用dm6数据库时发现不支持正则表达式,无法取字符串中整型数字,故通过一些基础的函数组合去实现。
CREATE or replace
FUNCTION "模式名"."GET_NUM_F1117"
(b1 in varchar2)
return int
is
count_1 int; --传入字符串的字符数
count_2 int; --当前的迭代位置,最大值为字符串的总字符数
count_3 int; --当前字符的ASCII值
count_4 int; --当前已经获取的阿拉伯数字
begin
count_1:=length(b1);
count_2:=1;
count_4:=-1;
loop --获取从左到右起第一个数字字符
exit
when count_1 < count_2;
count_3 =ascii(substr(b1,count_2,1));
exit
when count_3>=48 and count_3<=57;
count_2 :=count_2+1;
end loop;
if count_3>=48 and count_3<=57
then
count_4:=char(count_3);
count_2 :=count_2+1;
loop --进一步判断,如果是连续的阿拉伯数字组成的子串,则会进一步校对数值
exit
when count_1 < count_2;
count_3 =ascii(substr(b1,count_2,1));
exit
when count_3< 48 or count_3>57;
count_4 :=count_4*10+char(count_3);
count_2 :=count_2 +1;
end loop;
return count_4; --输出从左到右第一个数字值
else
return -1; --没有数字时输出-1
end if;
end;
执行时编辑需要的模式名和函数名
执行结果: