CREATE OR REPLACE FUNCTION FUN_CHECKIDCARD(PI_AAC002 VARCHAR2)
RETURN VARCHAR2 IS
/******************************************************************************
* 程序名:身份证合法性校验FUN_CHECKIDCARD
* 程序功能:对人员的身份证合法性进行校验
* 输入:身份证号码
* 输出:返回值: 0 合法;
* 版本日期:2015-01-16
* 执行类型:用户手工执行
* 修改过程:
******************************************************************************/
TYPE UDT_CHECK IS TABLE OF NUMBER;
UDO_CHECK UDT_CHECK := UDT_CHECK(7,
9,
10,
5,
8,
4,
2,
1,
6,
3,
7,
9,
10,
5,
8,
4,
2); --初始化校验位
L_RESULT NUMBER := 0;
L_STR NUMBER;
BEGIN
--15位身份证
IF LENGTHB(PI_AAC002) = 15 THEN
--身份证号不对
IF FUN_ISDATE('19' || SUBSTR(PI_AAC002, 7, 6)) = 0 THEN
RETURN 1;
END IF;
IF FUN_ISNUMBER(PI_AAC002) = 0 THEN
RETURN 2;
END IF;
--18位身份证
ELSIF LENGTHB(PI_AAC002) = 18 THEN
IF FUN_ISDATE(SUBSTR(PI_AAC002, 7, 8)) = 0 THEN
RETURN 3;
END IF;
IF FUN_ISNUMBER(SUBSTR(PI_AAC002, 1, 17)) = 0 THEN
RETURN 4;
END IF;
IF FUN_ISNUMBER(SUBSTR(PI_AAC002, 18, 1)) = 0 OR
SUBSTR(PI_AAC002, 18, 1) <> 'x' OR SUBSTR(PI_AAC002, 18, 1) <> 'X' THEN
RETURN 5;
END IF;
--验证校验位开始
FOR I IN 1 .. LENGTH(PI_AAC002) - 1 LOOP
L_STR := SUBSTR(PI_AAC002, I, 1);
L_RESULT := L_RESULT + L_STR * UDO_CHECK(I);
END LOOP;
L_STR := MOD(L_RESULT, 11);
SELECT DECODE(L_STR,
0,
1,
1,
0,
2,
'X',
3,
9,
4,
8,
5,
7,
6,
6,
7,
5,
8,
4,
9,
3,
10,
2)
INTO L_RESULT
FROM DUAL;
IF SUBSTR(PI_AAC002, LENGTH(PI_AAC002), 1) <> L_RESULT THEN
RETURN 7;
ELSE
RETURN 0;
END IF;
ELSE
RETURN 6;
END IF;
RETURN 0;
END FUN_CHECKIDCARD;
其中FUN_ISDATE 与FUN_ISNUMBER分别是判断是否是时间或着数据的函数:
CREATE OR REPLACE FUNCTION FUN_ISDATE(PI_PARAMETER VARCHAR2)
RETURN VARCHAR2 IS
L_VAL DATE;
BEGIN
L_VAL := TO_DATE(NVL(PI_PARAMETER, 'a'), 'yyyy-mm-dd hh24:mi:ss');
RETURN 1;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END FUN_ISDATE;
CREATE OR REPLACE FUNCTION FUN_ISNUMBER(PI_PARAMETER VARCHAR2)
RETURN VARCHAR2 IS
L_VAL NUMBER;
BEGIN
L_VAL := TO_NUMBER(NVL(PI_PARAMETER, 'a'));
RETURN 1;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END FUN_ISNUMBER;