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.