2

How to calculate a GS1 check digit for GTIN and SSCC codes in SQL Server.

3 Answers 3

4

This User-Defined Function will calculate check digits for all of the mentioned GTIN and SSCC formats on the GS1 website. The function will return the code that includes the check digit as a last character.

CREATE FUNCTION [GtinCheckDigit] (@Input VARCHAR(17))
RETURNS TABLE WITH SCHEMABINDING AS
RETURN WITH [ReverseInput](S) AS (
    SELECT REVERSE(@Input)
), [CharCount](N) AS (
    SELECT n from (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15),(16),(17)) a(n)
), [CharPos](N,S) AS (
    SELECT TOP (LEN(@Input)) [CharCount].N,SUBSTRING([ReverseInput].S,[CharCount].N,1)
    FROM [CharCount],[ReverseInput]
), [Multiplier](N) AS (
    SELECT (S*CASE WHEN (N%2) = 0 THEN 1 ELSE 3 END)
    FROM [CharPos]
), [Checksum](N) AS (
    SELECT CASE WHEN (SUM(N)%10) > 0 THEN (10-(SUM(N)%10)) ELSE 0 END
    FROM [Multiplier]
)
SELECT @Input + CAST(N as VARCHAR) as [Output] from [Checksum];

If you only need to retreive the calculated check digit you can change the last line of the function to something like this:

SELECT N from [Checksum];

This function will only work on SQL-Server 2008 or higher because of the REVERSE function that is being used to reverse the input.

1

In case you are looking for PostgreSQL GS1 check digit calculation under this thread:

CREATE OR REPLACE FUNCTION gs1_check_digit(code TEXT)
RETURNS TEXT
AS $$
#variable_conflict error
DECLARE
    digit SMALLINT;
    digit_3x BOOL DEFAULT TRUE;
    digit_sum SMALLINT DEFAULT 0;
    digit_index SMALLINT DEFAULT 1;
    digit_count SMALLINT;
BEGIN

    IF code IS NULL THEN
        RETURN '';
    END IF;

    digit_count := LENGTH(code);
    IF digit_count = 0 THEN
        RETURN '';
    END IF;

    code := REVERSE(code);

    LOOP
        digit := SUBSTRING(code, digit_index, 1)::SMALLINT;
        IF digit_3x THEN
            digit := digit*3;
        END IF;
        digit_sum := digit_sum + digit;
        digit_index := digit_index + 1;
        IF digit_index > digit_count THEN
            EXIT;
        END IF;
        digit_3x = (NOT digit_3x);
    END LOOP;

    RETURN CASE WHEN digit_sum%10=0 THEN digit_sum ELSE (digit_sum/10+1)*10 END - digit_sum;

END;
$$ LANGUAGE PLPGSQL;
2
  • This is a SQL Server question. Please post a PostgreSQL version in a seperate question.
    – Martin
    Commented Nov 23, 2020 at 8:39
  • 2
    The only thread comes after search for PostgreSQL is this Stackoverflow question, this is for lost PostgreSQL people
    – BIOHAZARD
    Commented Apr 2, 2021 at 20:41
1
CREATE OR ALTER FUNCTION dbo.udfGetUPCCheckDigit
(
  @Code AS VARCHAR(50)
)
RETURNS int
AS 
BEGIN
Declare @ret int;
WITH Totals (n) AS
(
    SELECT TOP (LEN(@Code))
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0)) a(n)
        CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
        CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
        CROSS JOIN (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d(n)
)
SELECT @ret =  (10 -
        SUM(CASE n%2 
            WHEN 1 THEN 3 
            ELSE 1 END * SUBSTRING(@Code, n, 1))
        % 10
        ) % 10 
FROM Totals;


Return @ret
END;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.