oracle银行卡卡号计算函数

create or replace function GetCardNoBySerialNo(v_sysacc varchar2,v_position number)
return varchar2
IS
v_i number(10);
v_j number(10);
v_ret number(10);
v_ret1 number(10);
v_ret2 number(10);
v_res number(10);
v_total number(10);
v_cardno varchar2(16);
BEGIN
v_j:=1;
v_i:=length(v_sysacc);
v_total :=0;
while v_i> 0 loop
   -- DBMS_OUTPUT.PUT_LINE('no=['||substr(v_sysacc,v_i,1)||']');
    if(mod(v_j,2) = 1) then
      v_ret2:=0;
      v_ret:=to_number(substr(v_sysacc,v_i,1))*2;

      if(v_ret >= 10) then
        v_ret1 := floor(v_ret/10) + mod(v_ret,10);
      else
        v_ret1 :=v_ret;
      end if;
       --DBMS_OUTPUT.PUT_LINE('1v_ret=['||to_char(v_ret1)||']');
    else
      v_ret2:=to_number(substr(v_sysacc,v_i,1));
      --DBMS_OUTPUT.PUT_LINE('2v_ret=['||to_char(v_ret2)||']');
      v_ret1:=0;
    end if;
    v_total :=v_total + v_ret1 + v_ret2;
    v_j:=v_j + 1;
    v_i := v_i - 1;
    --DBMS_OUTPUT.PUT_LINE('v_total['||to_char(v_total)||']==');
end loop;

v_res:=mod(10-mod(v_total,10),10);
--DBMS_OUTPUT.PUT_LINE('====['||to_char(v_res)||']==');

v_cardno:=substr(v_sysacc,1,v_position-1)||to_char(v_res)||substr(v_sysacc,v_position,16-v_position);
return v_cardno;
end GetCardNoBySerialNo;

posted on 2015-07-03 11:10  阮減显  阅读(217)  评论(0编辑  收藏  举报

导航