Postada em 21/01/2010 11:27 hs
--sql-server
CREATE FUNCTION dbo.fn_ValidaEAN ( @CodeEAN BIGINT ) RETURNS INT AS BEGIN DECLARE @dig1 INT, @dig2 INT, @dig3 INT, @dig4 INT, @dig5 INT, @dig6 INT, @dig7 INT, @dig8 INT, @dig9 INT, @dig10 INT, @dig11 INT, @dig12 INT, @CodeEANc VARCHAR(20), @soma_dig_par INT, @soma_dig_impar INT, @soma_dig INT, @resultado INT, @digito_ean INT
SELECT @CodeEANc = CAST(@CodeEAN AS VARCHAR) SELECT @dig1 = ( SUBSTRING(@CodeEANc, 1, 1) ), @dig2 = ( SUBSTRING(@CodeEANc, 2, 1) ), @dig3 = ( SUBSTRING(@CodeEANc, 3, 1) ), @dig4 = ( SUBSTRING(@CodeEANc, 4, 1) ), @dig5 = ( SUBSTRING(@CodeEANc, 5, 1) ), @dig6 = ( SUBSTRING(@CodeEANc, 6, 1) ), @dig7 = ( SUBSTRING(@CodeEANc, 7, 1) ), @dig8 = ( SUBSTRING(@CodeEANc, 8, 1) ), @dig9 = ( SUBSTRING(@CodeEANc, 9, 1) ), @dig10 = ( SUBSTRING(@CodeEANc, 10, 1) ), @dig11 = ( SUBSTRING(@CodeEANc, 11, 1) ), @dig12 = ( SUBSTRING(@CodeEANc, 12, 1) )
SELECT @soma_dig_par = 3 * ( @dig2 + @dig4 + @dig6 + @dig8 + @dig10 + @dig12 ), @soma_dig_impar = @dig1 + @dig3 + @dig5 + @dig7 + @dig9 + @dig11 SELECT @soma_dig = @soma_dig_par + @soma_dig_impar SELECT @resultado = ( ( 1 + Cast(@soma_dig / 10 AS INT) ) * 10 ) - @soma_dig
IF ( @resultado = 10 ) RETURN 0 ELSE RETURN @resultado RETURN 0 END
|