SET TERM ^ ;

CREATE OR ALTER procedure DIGITOEAN (
    NUMERO char(15))
returns (
    DIGITO numeric(1,0))
as
declare variable SOMA integer;
begin

    
        SOMA = 0;

        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  1 FOR 1)) as Integer)*1;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  2 FOR 1)) as Integer)*3;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  3 FOR 1)) as Integer)*1;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  4 FOR 1)) as Integer)*3;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  5 FOR 1)) as Integer)*1;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  6 FOR 1)) as Integer)*3;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  7 FOR 1)) as Integer)*1;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  8 FOR 1)) as Integer)*3;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM  9 FOR 1)) as Integer)*1;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM 10 FOR 1)) as Integer)*3;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM 11 FOR 1)) as Integer)*1;
        SOMA = :SOMA + Cast((SubString( :NUMERO FROM 12 FOR 1)) as Integer)*3;

        IF ((((:SOMA/10.00)-(:SOMA/10)) *10)=0) THEN
           BEGIN
             DIGITO =  0 ;
           END
           ELSE
           BEGIN
             DIGITO =   10-( ((:SOMA/10.00)-(:SOMA/10)) *10   ) ;
           END

    SUSPEND;
end
^

SET TERM ; ^


