You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 21, 2025. It is now read-only.
create or replacefunctionis_valid_gtin(gtin text) returns booleanas $$
declare
gtin_length int;
check_digit int;
gtin_without_check_digit text;
gtin_reverse text;
gtin_digits int[];
i int;
sum int :=0;
computed_check_digit int;
begin-- remove non-numeric characters
gtin := regexp_replace(gtin, '\\d', '', 'g');
gtin_length := length(gtin);
-- ensure gtin length is valid (8, 12, 13, or 14 digits)
if gtin_length not in (8, 12, 13, 14) then
return false;
end if;
gtin_without_check_digit := left(gtin, gtin_length -1);
check_digit := cast(right(gtin, 1) asint);
-- reverse the gtin (excluding check digit) and convert to an array of integers
gtin_reverse := reverse(gtin_without_check_digit);
gtin_digits := string_to_array(gtin_reverse, null)::int[];
-- compute the check digit using the luhn algorithm
for i in1..array_upper(gtin_digits, 1) loop
sum := sum + gtin_digits[i] * case i % 2 when 0 then 3 else 1 end;
end loop;
computed_check_digit := (10- sum % 10) % 10;
-- compare computed check digit with the actual check digit
return computed_check_digit = check_digit;
end;
$$ language plpgsql;
GTINs contain basic checksums we can use to verify that the data in the database is actually valid.
We could implement this at either the app or db level, run a script to mark any product with an invalid GTIN.
or