Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Check digit MOD 11 code not working

ae91dfd3-c84f-4778-9cbe-99b465997ad0Dec 9 2015 — edited Dec 9 2015

Set SERVEROUTPUT ON
DECLARE
  val_num NUMBER
:= '&user_input';
  holder NUMBER
:= 0;
  y NUMBER
:= 0;
  conv_string VARCHAR2
(20);
  
BEGIN
  conv_string
:= to_char(val_num*10);
  
for x in 1..length(conv_string) loop
  y
:= to_number(substr(conv_string, -x, 1));
  
if mod(x,2) = 0 then
  y
:= y * 2;
  
if y > 9 then
  y
:= y - 9;
  
end if;
  
end if;
  holder
:= holder + y;
  
end loop;
  dbms_output
.put_line ('Check is '||(11-Mod(holder, 11)));
END luhn;
  
/
SET SERVEROUTPUT ON


If you entered 036532 the check digit should be 7, but it is 2


the algorithm is in luhn

Steps to calculate the MOD11 check digit for a number such as an id:

  • Assign weights to each digit of the id #. The weights in MOD11 are from 2 through a maximum of 10 beginning with the low order position in the field.
  • Each digit in the id # is multiplied by its weight
  • The results of the multiplication are added together
  • This product is divided by the modulus number 11
  • The remainder is subtracted from the modulus number 11 giving the check digit

Steps to verify the check digit is included as part of the numner:If the remainder from the division is 0 or 1, then the subtraction will yield a two digit number of either 10 or 11. This won't work, so if the check digit is 10, then X is frequently used as the check digit and if the check digit is 11 then 0 is used as the check digit. If X is used, then the field for the check digit has to be defined as character (PIC X) or there will be a numeric problem.

  • The entire number is multiplied by the same weights that were used to calculate and the check digit itself is multiplied by 1.
  • The results of the multiplication are added together.
  • The sum is divided by 11 and if the remainder is 0, the number is correct.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2016
Added on Dec 9 2015
11 comments
1,280 views