Вот версия MySQL для EAN13:
SET @first12digits="123456789012";
SELECT @first12digits,
IF (
(@check:=10-MOD(
(CAST(SUBSTRING(@first12digits, 1, 1) AS DECIMAL))+
(CAST(SUBSTRING(@first12digits, 2, 1) AS DECIMAL) * 3)+
(CAST(SUBSTRING(@first12digits, 3, 1) AS DECIMAL))+
(CAST(SUBSTRING(@first12digits, 4, 1) AS DECIMAL) * 3)+
(CAST(SUBSTRING(@first12digits, 5, 1) AS DECIMAL))+
(CAST(SUBSTRING(@first12digits, 6, 1) AS DECIMAL) * 3)+
(CAST(SUBSTRING(@first12digits, 7, 1) AS DECIMAL))+
(CAST(SUBSTRING(@first12digits, 8, 1) AS DECIMAL) * 3)+
(CAST(SUBSTRING(@first12digits, 9, 1) AS DECIMAL))+
(CAST(SUBSTRING(@first12digits, 10, 1) AS DECIMAL) * 3)+
(CAST(SUBSTRING(@first12digits, 11, 1) AS DECIMAL))+
(CAST(SUBSTRING(@first12digits, 12, 1) AS DECIMAL) * 3)
,10)) = 10, 0, @check
) AS checkDigit;
Была ошибка. Если результат вычисления = 10, тогда проверьте цифру = 0.
Здесь ниже лучшая версия для EAN14.
SET @first13digits="1234567890123";
SELECT @txCode13:=@first13digits,
@iCheck := (
10 - (
(
MID(@txCode13, 2, 1) +
MID(@txCode13, 4, 1) +
MID(@txCode13, 6, 1) +
MID(@txCode13, 8, 1) +
MID(@txCode13, 10, 1) +
MID(@txCode13, 12, 1)
) + (
MID(@txCode13, 1, 1) +
MID(@txCode13, 3, 1) +
MID(@txCode13, 5, 1) +
MID(@txCode13, 7, 1) +
MID(@txCode13, 9, 1) +
MID(@txCode13, 11, 1) +
MID(@txCode13, 13, 1)
) * 3 ) % 10
) AS iCheck,
@iCheckDigit := IF(@iCheck = 10, 0, @iCheck) AS checkDigit,
CONCAT(@t
xCode13, CAST(@iCheckDigit AS CHAR)) AS EAN14WithCheck