Saturday, 25 June 2016

Calculate SSCC Check Digit

The last digit of a bar code number is a computer check digit which makes sure the bar code is correctly composed. Below is Logic for generating the Check Digit for SSCC number

The following table gives an example to illustrate how a Check Digit is calculated











Below i have created Stored Procedure to perform the check digit algorithm for SSCC

CREATE PROCEDURE [dbo].[CHECK_DIGIT]

(@PALLET_ID VARCHAR(100),@CHECKDIGIT_TOTAL VARCHAR(20) OUTPUT)
AS
BEGIN
DECLARE @LENGTH INT = 0,
                      @i TINYINT = 0,
                      @evenDigitSum INT = 0,
@oddDigitSum INT = 0,
                      @EA INT = 0,
                      @SumOfDigit INT = 0,
                      @TOTALCHECKDIGIT INT = 0,
                      @CHECKDIGIT INT = 0,
@NEAREST_TEN INT = 0,
                      @ROUNDVALUE INT = 0,
                      @ADD_ZERO INT = 0,
                      @CONVERT_STRING VARCHAR(10),
                      @CONVERT_INT INT = 0,
@TOTAL_CHECK_DIGIT INT  = 0

SET @LENGTH = LEN(RTRIM(LTRIM(@PALLET_ID)))

SET @i = 1
WHILE (@i <= @LENGTH)
BEGIN

SET @EA = @i % 2
IF @EA = 0
BEGIN
SET @evenDigitSum = CONVERT(TINYINT, SUBSTRING(@PALLET_ID,@i,1))
SET @evenDigitSum = @evenDigitSum * 3
SET @SumOfDigit = @evenDigitSum
END
ELSE
BEGIN
SET @oddDigitSum = CONVERT(TINYINT, SUBSTRING(@PALLET_ID,@i,1))
SET @oddDigitSum = @oddDigitSum * 1
SET @SumOfDigit = @oddDigitSum
END
SET @TOTALCHECKDIGIT = @TOTALCHECKDIGIT + @SumOfDigit
SET @i = @i + 1
END

SET @NEAREST_TEN = @TOTALCHECKDIGIT % 10
SET @ROUNDVALUE = ROUND(@NEAREST_TEN,2)
IF @ROUNDVALUE > 1 
BEGIN  
SET @CONVERT_INT = 10 - @ROUNDVALUE
END
ELSE
BEGIN
SET @CONVERT_INT = 0
END 
SET @TOTAL_CHECK_DIGIT = @CONVERT_INT
SET @CHECKDIGIT_TOTAL  = CONVERT(varchar(20), @TOTAL_CHECK_DIGIT)
RETURN @CHECKDIGIT_TOTAL
END
GO

No comments:

Post a Comment