create procedure modul_10 (@@number varchar(15)) as begin /* **procedure to calculate modulus-10 checkdigit **procedure written 19980831 by andreas birgerson **last revision on 19980831 by andreas birgerson */ set nocount on declare @@max smallint declare @@counter smallint declare @@weight varchar(24) declare @@rest smallint declare @@value smallint select @@weight='121212121212121212121212' select @@max=datalength(@@number) select @@weight=right(@@weight,@@max) if not @@max between 2 and 15 begin raiserror('Modul-10 invalid argument length',2,-1) with seterror return end create table #f (chkdigit smallint null) select @@counter=@@max+1 loop: select @@counter=@@counter-1 select @@value= (ascii(substring(@@weight,@@counter,1))-48)* (ascii(substring(@@number,@@counter,1)) -48) insert #f values (case when (@@value<10) then @@value when (@@value>9) then @@value-10 end) insert #f values (case when (@@value<10) then null when (@@value>9) then 1 end) if @@counter >1 goto loop select @@rest=sum(chkdigit)%10 from #f select "Checkdigit"=case @@rest when 0 then 0 else 10-@@rest end drop table #f set nocount off end go