Oracle ID Check bit to determine if string is a number function ___ static function

Source: Internet
Author: User
A function of the last ID card check code is obtained by entering the first 17 ID number, and the output is the value of the checksum code.
Create or Replace function Getcheckcode (Identity_code VARCHAR2) return VARCHAR2/*****author:mr_wu ******version:1.0.0. 1 ******function: Computes the last validated bit ******param:input identity_code 17-bit or 18-bit ******/as Last_co by entering the 17-bit or 18-digit number      De VARCHAR2 (255);--Returns the string sum_num number;
--Flag number (1) of the product of the position right and the corresponding bit value; Notenoughlengthinput EXCEPTION--Exception handling, input parameters conformance to requirements BEGIN--to determine whether a digital SELECT isnumeric (substr (identity_code,1,17)) in
    to flag from dual;
    IF flag = 0 THEN RAISE notenoughlengthinput;
    End If;
    --Initialization sum variable sum_num: = 0;  
          --Determine if the bit length is up to the identity card requirement if length (identity_code) = or length (identity_code) = THEN for i in 1..17 loop
      Sum_num: = Sum_num + substr (identity_code,i,1) *mod (Power (2, (18-i)), 11);
      End LOOP;
                                    --SELECT Decode (MOD (sum_num,11), 0, ' 1 ', 1, ' 0 ') according to the computed and in the modulo operation.
  2, ' X ', 3, ' 9 ',                                  4, ' 8 ', 5, ' 7 ', 6,
                                    ' 6 ', 7, ' 5 ', 8, ' 4 ',
      9, ' 3 ', ten, ' 2 ') into the last_code from dual; IF LENGTH (Identity_code) = substr (identity_code,18,1) <> last_code THEN last_code: = last_code| | ', you lose
      The last authentication bit is incorrect! ';
    End IF;
    ELSE RAISE Notenoughlengthinput;
  End If; Return ' The last verification bit is: ' | | 
  Last_code;
      --Exception capture EXCEPTION when Notenoughlengthinput THEN last_code: = ' Please enter 17-bit or 18-digit number to verify! '; 
return last_code; End Getcheckcode;


A function that determines whether the first 17-bit string entered is a number:

CREATE OR REPLACE FUNCTION isnumeric (str in VARCHAR2) return number are
------------return
0 not a number or null   Return 1 digit-------------
       v_str number;
BEGIN
       IF str is NULL
         THEN return
         0;
       ELSE
         BEGIN
           SELECT to_number (str) into
           v_str from
           DUAL;
         EXCEPTION when
           invalid_number
               THEN return
                 0;
               End;
         return 1;
       End IF;
End IsNumeric;


Effect Chart:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.