PLSQL实现函数取varchar中的数字

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;

执行时编辑需要的模式名和函数名
在这里插入图片描述
执行结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值