SQL proc计算7和12位upc的校验位

时间:2014-01-14 23:55:55

标签: sql tsql barcode checksum

我不得不再次在互联网上搜索此代码,所以我想我会把它放在这里,所以我可以在下次更快地找到它并希望你发现它也快一点了:)

5 个答案:

答案 0 :(得分:1)

CREATE FUNCTION [dbo].[fn_EAN13CheckDigit] (@Barcode nvarchar(12)) 
RETURNS nvarchar(13) 
AS 
BEGIN 
    DECLARE @SUM int , @COUNTER int, @RETURN varchar(13), @Val1 int, @Val2 int 
    SET @COUNTER = 1 SET @SUM = 0 

    WHILE @Counter < 13 
    BEGIN 
        SET @VAL1 = SUBSTRING(@Barcode,@counter,1) * 1 
        SET @VAL2 = SUBSTRING(@Barcode,@counter + 1,1) * 3 
        SET @SUM = @VAL1 + @SUM 
        SET @SUM = @VAL2 + @SUM 
        SET @Counter = @Counter + 2; 
    END 
    SET @SUM=(10-(@SUM%10))%10

    SET @Return = @BARCODE + CONVERT(varchar,((@SUM))) 
    RETURN @Return 
END

答案 1 :(得分:1)

检查一下。 以下代码可以检查所有GTIN中的数字(EAN8,EAN13,EAN14,UPC / A,UPC / E)

CREATE FUNCTION [dbo].[check_digit]
(
    @GTIN VARCHAR(14)
)
RETURNS TINYINT
AS
BEGIN
    DECLARE @Index TINYINT,
            @Multiplier TINYINT,
            @Sum TINYINT,
            @checksum TINYINT,
            @result TINYINT,
            @GTIN_strip VARCHAR(13)

    SELECT  @GTIN_strip = SUBSTRING(@GTIN,1,LEN(@GTIN)-1);

    SELECT  @Index = LEN(@GTIN_strip),
            @Multiplier = 3,
            @Sum = 0

    WHILE @Index > 0
        SELECT  @Sum = @Sum + @Multiplier * CAST(SUBSTRING(@GTIN_strip, @Index, 1) AS TINYINT),
            @Multiplier = 4 - @Multiplier,
            @Index = @Index - 1

    SELECT @checksum = CASE @Sum % 10
            WHEN 0 THEN '0'
            ELSE CAST(10 - @Sum % 10 AS CHAR(1))
        END


    IF (SUBSTRING(@GTIN,LEN(@GTIN),1) = @checksum)
        RETURN 1; /*true*/
        RETURN 0; /*false*/
END

答案 2 :(得分:1)

这也是一个MySQL实现。 请注意,它需要13位UPC / EAN13作为输入,因此您应该根据需要对输入进行LPAD。

drop function if exists barcodify;
delimiter //
 at the last digit.
create function barcodify(upc varchar(15))
    returns varchar(15)
    sql security invoker
begin
    declare i, r, odd, even,result int;

    set odd=0;
    set even =0;
    set i = length(upc);

    while i > 0 do
        set r = substring(upc, i, 1) ;
        if(i % 2 >0) then set odd = odd + r;
        else set even = even + r;
        end if;
        set i = i - 1;
    end while;
    set result = (3*odd)+even;

    if result % 10 =0 then return 0;
    else return (10 - (result %10));
    end if;
end //

delimiter ;

答案 3 :(得分:1)

这是我在Oracle中实现一个函数。

CREATE OR REPLACE FUNCTION GenerateCheckDigit(pUPC IN VARCHAR2) RETURN INT
  IS
  vCheckDigit INT;
  BEGIN

    WITH barcodeData AS 
    (
      SELECT pUPC barcode FROM dual
    )
    SELECT       
      10-REPLACE(MOD(SUM(SUBSTR(barcode, -LEVEL, 1) * DECODE(MOD(LEVEL-1, 2), 1, 1, 3)),10),0,10) INTO vCheckDigit
      FROM barcodeData 
    CONNECT BY LEVEL <= LENGTH(barcode);

    RETURN vCheckDigit;

  END;

答案 4 :(得分:0)

CREATE  FUNCTION sfn_ean_chkdigit(@barcode varchar(20))
RETURNS CHAR(1)
AS
BEGIN
    DECLARE @chk_digit int, @chk int
    DECLARE @num TABLE (num int)

    IF LEN(@barcode) NOT IN (7, 12) RETURN NULL

    INSERT INTO @num 
    SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL  
    SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL 
    SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12

    SELECT @chk_digit = SUM(CONVERT(int, SUBSTRING(@barcode, LEN(@barcode) - num + 1, 1)) * CASE WHEN num % 2 = 1 THEN 3 ELSE 1 END)
    FROM   @num
    WHERE  num    <= LEN(@barcode)

    SELECT @chk_digit = (10 - (@chk_digit % 10)) % 10

    RETURN  CHAR(ASCII('0') + @chk_digit)
END