1、正则表达式写法:

CREATE OR REPLACE FUNCTION Func_checkidcard (p_idcard IN VARCHAR2) RETURN INT
IS
v_regstr VARCHAR2 (2000);
v_sum NUMBER;
v_mod NUMBER;
v_checkcode CHAR (11) := '10X98765432';
v_checkbit CHAR (1);
v_areacode VARCHAR2 (2000) := '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,';
BEGIN
CASE LENGTHB (p_idcard)
WHEN 15
THEN -- 15位
IF INSTRB (v_areacode, SUBSTR (p_idcard, 1, 2) || ',') = 0 THEN
RETURN 0;
END IF; IF MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 2)) + 1900, 400) = 0
OR
(
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 2)) + 1900, 100) <> 0
AND
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 2)) + 1900, 4) = 0
)
THEN -- 闰年
v_regstr :=
'^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$';
ELSE
v_regstr :=
'^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$';
END IF; IF REGEXP_LIKE (p_idcard, v_regstr) THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
WHEN 18
THEN -- 18位
IF INSTRB (v_areacode, SUBSTRB (p_idcard, 1, 2) || ',') = 0 THEN
RETURN 0;
END IF; IF MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 4)), 400) = 0
OR
(
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 4)), 100) <> 0
AND
MOD (TO_NUMBER (SUBSTRB (p_idcard, 7, 4)), 4) = 0
)
THEN -- 闰年
v_regstr :=
'^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$';
ELSE
v_regstr :=
'^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$';
END IF; IF REGEXP_LIKE (p_idcard, v_regstr) THEN
v_sum :=
( TO_NUMBER (SUBSTRB (p_idcard, 1, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 11, 1))
)
* 7
+ ( TO_NUMBER (SUBSTRB (p_idcard, 2, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 12, 1))
)
* 9
+ ( TO_NUMBER (SUBSTRB (p_idcard, 3, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 13, 1))
)
* 10
+ ( TO_NUMBER (SUBSTRB (p_idcard, 4, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 14, 1))
)
* 5
+ ( TO_NUMBER (SUBSTRB (p_idcard, 5, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 15, 1))
)
* 8
+ ( TO_NUMBER (SUBSTRB (p_idcard, 6, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 16, 1))
)
* 4
+ ( TO_NUMBER (SUBSTRB (p_idcard, 7, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 17, 1))
)
* 2
+ TO_NUMBER (SUBSTRB (p_idcard, 8, 1)) * 1
+ TO_NUMBER (SUBSTRB (p_idcard, 9, 1)) * 6
+ TO_NUMBER (SUBSTRB (p_idcard, 10, 1)) * 3;
v_mod := MOD (v_sum, 11);
v_checkbit := SUBSTRB (v_checkcode, v_mod + 1, 1); IF v_checkbit = upper(substrb(p_idcard,18,1)) THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 0; -- 身份证号码位数不对
END CASE;
EXCEPTION
WHEN OTHERS
THEN
RETURN 0;
END fn_checkidcard;
/
Show Err;

2、非正则表达式写法

Create Or Replace Function Func_checkIdcard (p_idcard in varchar2) Return Number
Is
v_sum Number;
v_mod Number;
v_length Number;
v_date Varchar2(10);
v_isDate Boolean;
v_isNumber Boolean;
v_isNumber_17 Boolean;
v_checkbit CHAR (1);
v_checkcode CHAR (11) := '10X98765432';
v_areacode VARCHAR2 (2000) := '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91,'; --[isNumber]--
Function isNumber (p_string in varchar2) Return Boolean
Is
i number;
k number;
flag boolean;
v_length number;
Begin
/*
算法:
通过ASCII码判断是否数字,介于[48, 57]之间。
select ascii('0'),ascii('1'),ascii('2'),ascii('3'),ascii('4'),ascii('5'),ascii('6'),ascii('7'),ascii('8'),ascii('9') from dual;
*/ flag := True;
select length(p_string) into v_length from dual; for i in 1..v_length loop
k := ascii(substr(p_string,i,1));
if k < 48 or k > 57 then
flag := False;
Exit;
end if;
end loop; Return flag;
End isNumber; --[isDate]--
Function isDate (p_date in varchar2) Return Boolean
Is
v_flag boolean;
v_year number;
v_month number;
v_day number;
v_isLeapYear boolean;
Begin
--[初始化]--
v_flag := True; --[获取信息]--
v_year := to_number(substr(p_date,1,4));
v_month := to_number(substr(p_date,5,2));
v_day := to_number(substr(p_date,7,2)); --[判断是否为闰年]--
if (mod(v_year,400) = 0) Or (mod(v_year,100) <> 0 And mod(v_year,4) = 0) then
v_isLeapYear := True;
else
v_isLeapYear := False;
end if; --[判断月份]--
if v_month < 1 Or v_month > 12 then
v_flag := False;
Return v_flag;
end if; --[判断日期]--
if v_month in (1,3,5,7,8,10,12) and (v_day < 1 or v_day > 31) then
v_flag := False;
end if;
if v_month in (4,6,9,11) and (v_day < 1 or v_day > 30) then
v_flag := False;
end if;
if v_month in (2) then
if (v_isLeapYear) then
--[闰年]--
if (v_day < 1 or v_day > 29) then
v_flag := False;
end if;
else
--[非闰年]--
if (v_day < 1 or v_day > 28) then
v_flag := False;
end if;
end if;
end if; --[返回结果]--
Return v_flag;
End isDate;
Begin
/*
返回值说明:
-1 身份证号码位数不对
-2 身份证号码出生日期超出范围
-3 身份证号码含有非法字符
-4 身份证号码校验码错误
-5 身份证号码地区码非法
1 身份证号码通过校验
*/
--[长度校验]--
if p_idcard is null then
return -1;
end if ;
select lengthb(p_idcard) into v_length from dual;
if v_length not in (15,18) then
return -1;
end if; --[区位码校验]--
if instrb(v_areacode, substr(p_idcard, 1, 2)||',') = 0 then
return -5;
end if; --[格式化校验]--
if v_length = 15 then
v_isNumber := isNumber (p_idcard);
if not (v_isNumber) then
return -3;
end if;
elsif v_length = 18 then
v_isNumber := isNumber (p_idcard);
v_isNumber_17 := isNumber (substr(p_idcard,1,17));
if not ((v_isNumber) or (v_isNumber_17 and upper(substr(p_idcard,18,1)) = 'X')) then
return -3;
end if;
end if; --[出生日期校验]--
if v_length = 15 then
select ''||substr(p_idcard,7,6) into v_date from dual;
elsif v_length = 18 then
select substr(p_idcard,7,8) into v_date from dual;
end if;
v_isDate := isDate (v_date);
if not (v_isDate) then
return -2;
end if; --[校验码校验]--
if v_length = 18 then
v_sum :=
( TO_NUMBER (SUBSTRB (p_idcard, 1, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 11, 1))
)
* 7
+ ( TO_NUMBER (SUBSTRB (p_idcard, 2, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 12, 1))
)
* 9
+ ( TO_NUMBER (SUBSTRB (p_idcard, 3, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 13, 1))
)
* 10
+ ( TO_NUMBER (SUBSTRB (p_idcard, 4, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 14, 1))
)
* 5
+ ( TO_NUMBER (SUBSTRB (p_idcard, 5, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 15, 1))
)
* 8
+ ( TO_NUMBER (SUBSTRB (p_idcard, 6, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 16, 1))
)
* 4
+ ( TO_NUMBER (SUBSTRB (p_idcard, 7, 1))
+ TO_NUMBER (SUBSTRB (p_idcard, 17, 1))
)
* 2
+ TO_NUMBER (SUBSTRB (p_idcard, 8, 1)) * 1
+ TO_NUMBER (SUBSTRB (p_idcard, 9, 1)) * 6
+ TO_NUMBER (SUBSTRB (p_idcard, 10, 1)) * 3;
v_mod := MOD (v_sum, 11);
v_checkbit := SUBSTRB (v_checkcode, v_mod + 1, 1); if v_checkbit = upper(substrb(p_idcard,18,1)) then
return 1;
else
return -4;
end if;
else
return 1;
end if;
End Func_checkIdcard;
/
Show Err;

oracle 身份证校验函数的更多相关文章

  1. Oracle 身份证校验

    SELECT SUBSTR('&AI', 0, 17) || TRANSLATE(DECODE(MOD(SUM(SUBSTR(WI, REGEXP_INSTR(WI, ' ', 1, I) + ...

  2. 完美实现身份证校验 js正则

    注意: 1.只针对18为身份证号码进行校验,现在15位的应该很少了, 2.不区分xX大小写, 3.出生年份1900-2099,每月的天数也进行相关验证(考虑的闰月的情况), 4.校验规则详见,这个写的 ...

  3. Java 常用正则表达式,Java正则表达式,Java身份证校验,最新手机号码正则表达式

    Java 常用正则表达式,Java正则表达式,Java身份证校验,最新手机号码校验正则表达式 ============================== ©Copyright 蕃薯耀 2017年11 ...

  4. Oracle的nvl函数和nvl2函数

    一.基本语法 介绍一下oracle的nvl函数和nvl2函数. nvl函数 nvl函数基本语法为nvl(E1,E2),意思是E1为null就返回E2,不为null就返回E1. nvl2函数 nvl2函 ...

  5. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  6. 重写Oracle的wm_concat函数,自定义分隔符、排序

    oracle中,wm_concat函数是一个聚合函数,和mysql中的group_concat函数类似,不过group_concat函数比较强大,可以定义分隔符和排序,当然所谓强大是相对的,这里假使我 ...

  7. 篇二:JS身份证校验

    身份证校验 function identityCodeValid(code) { var city={11:"北京",12:"天津",13:"河北&q ...

  8. Oracle日期时间函数大全

    ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year: yy two digits 两位年 显示值:07 yyy three digits ...

  9. Oracle过程及函数的参数模式,In、out、in out模式

    Oracle过程及函数的参数模式 In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: ( ...

随机推荐

  1. windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码

    为什么使用PHP_CodeSniffer 一个开发团队统一的编码风格,有助于他人对代码的理解和维护,对于大项目来说尤其重要. PHP_CodeSniffer是PEAR中的一个用PHP5写的用来检查嗅探 ...

  2. 【解决】SharePoint 2013 with SP1安装问题及解决

    安装环境 操作系统:Windows Server 2008 R2 SP1 安装包:SharePoint Server 2013 with SP1 问题及解决 I. 必备组件(prerequisitei ...

  3. Andriod中textview垂直水平居中及LinearLayout内组件的垂直布局

    1.textview 垂直水平居中的设置 Android:gravity="center_vertical|center" 2.LinearLayout中设置控件垂直布局,默认的是 ...

  4. mysql下将分隔字符串转换为数组

    MySQL存储过程可以用于分割字符串,下面就为您详细介绍这种MySQL存储过程的用法,供您参考学习之用. 现有一段字符串,如apple,banana,orange,pears,grape,要把它按照逗 ...

  5. Entity Framework学习笔记——配置EF

    初次使用Entity Framework(以下简称EF),为了避免很快忘记,决定开日志记录学习过程和遇到的问题.因为项目比较小,只会用到EF的一些基本功能,因此先在此处制定一个学习目标:1. 配置EF ...

  6. 14,SFDC 管理员篇 - 外部数据集成

    1,Connect an External Data Source, 添加新的数据源,填写如下 2, 点击Validate and Sync 按钮 3,添加Orders和Orders Details ...

  7. MSMQ消息队列 用法

    引言 接下来的三篇文章是讨论有关企业分布式开发的文章,这三篇文章筹划了很长时间,文章的技术并不算新,但是文章中使用到的技术都是经过笔者研究实践后总结的,正所谓站在巨人的肩膀上,笔者并不是巨人,但也希望 ...

  8. MongoDB学习笔记(数据操作)

    1.  批量插入:     以数组的方式一次插入多个文档可以在单次TCP请求中完成,避免了多次请求中的额外开销.就数据传输量而言,批量插入的数据中仅包含一份消息头,而多次单条插入则会在每次插入数据时封 ...

  9. 深入理解CSS溢出overflow & overflow:hidden真的失效了吗[转载]

    深入理解CSS溢出overflow http://www.cnblogs.com/xiaohuochai/p/5289653.html overflow:hidden真的失效了吗 http://www ...

  10. js 表格实现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...