SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 EAN algorithms
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 08/24/2007 :  07:45:08  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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"

Edited by - SwePeso on 08/24/2007 07:57:36

Vaishu
Posting Yak Master

178 Posts

Posted - 08/24/2007 :  09:57:52  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000