Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 EAN algorithms

Author  Topic 

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-24 : 07:45:08
Here is how you get the check digit for EAN-8, EAN-13 and EAN-14.
CREATE FUNCTION dbo.fnGetEAN
(
@EAN VARCHAR(13)
)
RETURNS VARCHAR(14)
AS
BEGIN
DECLARE @Index TINYINT,
@Multiplier TINYINT,
@Sum TINYINT

SELECT @Index = LEN(@EAN),
@Multiplier = 3,
@Sum = 0

WHILE @Index > 0
SELECT @Sum = @Sum + @Multiplier * CAST(SUBSTRING(@EAN, @Index, 1) AS TINYINT),
@Multiplier = 4 - @Multiplier,
@Index = @Index - 1

RETURN CASE @Sum % 10
WHEN 0 THEN @EAN + '0'
ELSE @EAN + CAST(10 - @Sum % 10 AS CHAR(1))
END
END


E 12°55'05.25"
N 56°04'39.16"

Vaishu
Posting Yak Master

178 Posts

Posted - 2007-08-24 : 09:57:52
Hi

Thank you. About EAN I got confued first now I understood. I will use this function.

Thank you

quote:
Originally posted by Peso

Here is how you get the check digit for EAN-8, EAN-13 and EAN-14.
CREATE FUNCTION dbo.fnGetEAN
(
@EAN VARCHAR(13)
)
RETURNS VARCHAR(14)
AS
BEGIN
DECLARE @Index TINYINT,
@Multiplier TINYINT,
@Sum TINYINT

SELECT @Index = LEN(@EAN),
@Multiplier = 3,
@Sum = 0

WHILE @Index > 0
SELECT @Sum = @Sum + @Multiplier * CAST(SUBSTRING(@EAN, @Index, 1) AS TINYINT),
@Multiplier = 4 - @Multiplier,
@Index = @Index - 1

RETURN CASE @Sum % 10
WHEN 0 THEN @EAN + '0'
ELSE @EAN + CAST(10 - @Sum % 10 AS CHAR(1))
END
END


E 12°55'05.25"
N 56°04'39.16"

Go to Top of Page
   

- Advertisement -