1 create or replace function CHECK_IDCARD(p_idcard varchar2) return number is
  2 
  3  v_regstr VARCHAR2 (2000);
  4  v_sum NUMBER;
  5  v_mod NUMBER;
  6  v_checkcode CHAR (11) := '10X98765432';
  7  v_checkbit CHAR (1);
  8  v_areacode VARCHAR2 (2000)
  9  := '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,';
 10 BEGIN
 11  CASE LENGTHB (p_idcard)
 12  WHEN 15
 13  THEN -- 验证15位身份证
 14  IF INSTRB (v_areacode, SUBSTR (p_idcard, 1, 2) || ',') = 0
 15  THEN
 16  RETURN 0;
 17  END IF;
 18 
 19  IF MOD (TO_NUMBER (SUBSTR (p_idcard, 7, 2)) + 1900, 400) = 0
 20  OR ( MOD (TO_NUMBER (SUBSTR (p_idcard, 7, 2)) + 1900, 100) <>
 21  0
 22  AND MOD (TO_NUMBER (SUBSTR (p_idcard, 7, 2)) + 1900, 4) = 0
 23  )
 24  THEN -- 闰年
 25  v_regstr :=
 26  '^[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}$';
 27  ELSE
 28  v_regstr :=
 29  '^[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}$';
 30  END IF;
 31 
 32  IF REGEXP_LIKE (p_idcard, v_regstr)
 33  THEN
 34  RETURN 1;
 35  ELSE
 36  RETURN 0;
 37  END IF;
 38  WHEN 18
 39  THEN --验证 18位身份证
 40  IF INSTRB (v_areacode, SUBSTR (p_idcard, 1, 2) || ',') = 0
 41  THEN
 42  RETURN 0;
 43  END IF;
 44 
 45  IF MOD (TO_NUMBER (SUBSTR (p_idcard, 7, 4)), 400) = 0
 46  OR ( MOD (TO_NUMBER (SUBSTR (p_idcard, 7, 4)), 100) <> 0
 47  AND MOD (TO_NUMBER (SUBSTR (p_idcard, 7, 4)), 4) = 0
 48  )
 49  THEN -- 闰年
 50  v_regstr :=
 51  '^[1-9][0-9]0{5}19[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]$';
 52  ELSE
 53  v_regstr :=
 54  '^[1-9][0-9]{5}19[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]$';
 55  END IF;
 56 
 57  IF REGEXP_LIKE (p_idcard, v_regstr)
 58  THEN
 59  v_sum :=
 60  ( TO_NUMBER (SUBSTRB (p_idcard, 1, 1))
 61  + TO_NUMBER (SUBSTRB (p_idcard, 11, 1))
 62  )
 63  * 7
 64  + ( TO_NUMBER (SUBSTRB (p_idcard, 2, 1))
 65  + TO_NUMBER (SUBSTRB (p_idcard, 12, 1))
 66  )
 67  * 9
 68  + ( TO_NUMBER (SUBSTRB (p_idcard, 3, 1))
 69  + TO_NUMBER (SUBSTRB (p_idcard, 13, 1))
 70  )
 71  * 10
 72  + ( TO_NUMBER (SUBSTRB (p_idcard, 4, 1))
 73  + TO_NUMBER (SUBSTRB (p_idcard, 14, 1))
 74  )
 75  * 5
 76  + ( TO_NUMBER (SUBSTRB (p_idcard, 5, 1))
 77  + TO_NUMBER (SUBSTRB (p_idcard, 15, 1))
 78  )
 79  * 8
 80  + ( TO_NUMBER (SUBSTRB (p_idcard, 6, 1))
 81  + TO_NUMBER (SUBSTRB (p_idcard, 16, 1))
 82  )
 83  * 4
 84  + ( TO_NUMBER (SUBSTRB (p_idcard, 7, 1))
 85  + TO_NUMBER (SUBSTRB (p_idcard, 17, 1))
 86  )
 87  * 2
 88  + TO_NUMBER (SUBSTRB (p_idcard, 8, 1)) * 1
 89  + TO_NUMBER (SUBSTRB (p_idcard, 9, 1)) * 6
 90  + TO_NUMBER (SUBSTRB (p_idcard, 10, 1)) * 3;
 91  v_mod := MOD (v_sum, 11);
 92  v_checkbit := SUBSTRB (v_checkcode, v_mod + 1, 1);
 93 
 94  IF v_checkbit = SUBSTRB (p_idcard, 18, 1)
 95  THEN
 96  RETURN 1;
 97  ELSE
 98  RETURN 0;
 99  END IF;
100  ELSE
101  RETURN 0;
102  END IF;
103  ELSE
104  RETURN 0; -- 身份证号码位数不对
105  END CASE;
106 EXCEPTION
107  WHEN OTHERS
108  THEN
109  RETURN 0;
110 end CHECK_IDCARD;