create or replace function idsfz(i_sfz varchar2) return varchar2 is
type tiarray is table of integer;
type tcarray is table of char(1);
v_results varchar2(18);
v_w tiarray; --数字数组
v_a tcarray; --字符数组
v_s integer;
begin
if i_sfz is null then
return 'null';
end if;
if length(i_sfz) = 15 then
v_w:= tiarray(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
v_a:= tcarray('1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2');
v_results:= substr(i_sfz, 1, 6) || '19' || substr(i_sfz, 7, 9);
v_s := 0;
begin
for i in 1 .. 17 loop
v_s := v_s + to_number(substr(v_results, i, 1)) * v_w(i);
end loop;
exception
when others then
return 'error';
end ;
v_s:= v_s mod 11;
v_results := v_results || v_a(v_s + 1);
end if;
if length(i_sfz) = 18 then
v_results:= substr(i_sfz,1,6)||substr(i_sfz,9,9);
end if;
return(v_results);
end idsfz;
验证
select idsfz('521010102204344')
from sfz;
身份证的转换15to18,18to15
最新推荐文章于 2023-06-26 10:11:30 发布