600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Oracle验证身份证号码有效性

Oracle验证身份证号码有效性

时间:2022-02-01 19:09:09

相关推荐

Oracle验证身份证号码有效性

废话不多说,直接上代码

create or replace function f_get_idcard_address(p_idcode in varchar2)/*Create By yuxiao -04-27*/return varchar isv_area varchar2(50);beginselect areainto v_areafrom yuxiao.idcardwhere idcode = substr(p_idcode, 1, 6);return v_area;exceptionwhen others thenreturn null;end f_get_idcard_address;

create or replace function f_get_idcard_birthday(p_birthday in varchar2)/*Create By yuxiao -04-27*/return varchar isbeginreturn to_char(to_date(substr(p_birthday, 7, 8), 'yyyymmdd'),'yyyy"年"mm"月"dd"日"');exceptionwhen others thenreturn null;end f_get_idcard_birthday;

create or replace function f_get_idcard_sex(p_sex in varchar2)/*Create By yuxiao -04-27*/return varchar isbeginreturn case mod(substr(p_sex, 17, 1), 2) when 1 then '男' when 0 then '女' else null end;exceptionwhen others thenreturn null;end f_get_idcard_sex;

create or replace function f_get_idcard_check(p_idcode in varchar2) return varchar is/*Create By yuxiao -04-27*/type tiarray is table of integer;type tcarray is table of char(1);w tiarray; --数字数组a tcarray; --字符数组s number;beginw := tiarray(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);a := tcarray('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');s := 0;beginfor i in 1 .. 17 loops := s + to_number(substr(p_idcode, i, 1)) * w(i);end loop;exceptionwhen others thenreturn '';end;s:= s mod 11;if a(s + 1) = substr(p_idcode,-1) thenreturn 'ok';elsereturn a(s + 1);end if;exceptionwhen others thenreturn null;end f_get_idcard_check;

create or replace procedure prc_get_idcard_info(p_idcard in varchar2,out_str out varchar2) is/*Create By yuxiao -04-27*/v_address varchar2(50);v_birthday varchar2(50);v_sexvarchar2(10);v_check varchar2(10);v_out_str varchar2(2000);err_cnt number := 0;begin--格式校验if not regexp_like(nvl(p_idcard,'1'),'^\d{17}(\d|X)$') thenv_out_str := '无效的格式!' || v_check;out_str := out_str || v_out_str;err_cnt := err_cnt + 1;return;end if;--归属地v_address := f_get_idcard_address(p_idcard);if v_address is null thenv_out_str := '无效证件号码! 1~6位输入有误';out_str := out_str || v_out_str;err_cnt := err_cnt + 1;return;end if;--出生日v_birthday := f_get_idcard_birthday(p_idcard);if v_birthday is null thenv_out_str := '无效证件号码! 7~14位(出生日期)输入有误';out_str := out_str || v_out_str;err_cnt := err_cnt + 1;return;end if;--性别v_sex := f_get_idcard_sex(p_idcard);if v_sex is null thenv_out_str := '无效证件号码! 第17位输入有误';out_str := out_str || v_out_str;err_cnt := err_cnt + 1;return;end if;--末位校验码v_check := f_get_idcard_check(p_idcard);if v_check != 'ok' thenv_out_str := '无效证件号码! 最末位应该是' || v_check;out_str := out_str || v_out_str;err_cnt := err_cnt + 1;return;end if;if err_cnt = 0 thenv_out_str := v_out_str || '身份证号:' || p_idcard || chr(10);v_out_str := v_out_str || '证件地址:' || v_address || chr(10);v_out_str := v_out_str || '生 日:' || v_birthday || chr(10);v_out_str := v_out_str || '性 别:' || v_sex || chr(10);out_str := v_out_str;end if;exceptionwhen others thenv_out_str := '无效证件号码!';out_str := v_out_str;end prc_get_idcard_info;

运行效果如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。