Skip to content

Instantly share code, notes, and snippets.

@gabrielkfr
Created June 25, 2015 11:49
Show Gist options
  • Save gabrielkfr/b5a06d031d7797fd07bc to your computer and use it in GitHub Desktop.
Save gabrielkfr/b5a06d031d7797fd07bc to your computer and use it in GitHub Desktop.
Función PLSQL que permite calcular el dígito verificador de un código de barras EAN13.
CREATE OR REPLACE FUNCTION CODIGO_BARRAS_GENERAR_DV(P_CODIGO IN VARCHAR2, P_TIPO VARCHAR2 DEFAULT 'EAN13') RETURN VARCHAR2 IS
L_SUM NUMBER;
L_MULTIPLE NUMBER;
V_CODIGO_EAN13 VARCHAR2(13) DEFAULT NULL;
BEGIN
IF (P_TIPO = 'EAN13') THEN
V_CODIGO_EAN13 := SUBSTR(LPAD(P_CODIGO,12,'0'),1,12);
FOR I IN 1..12 LOOP
IF (MOD(I,2) = 0) THEN
L_MULTIPLE := 3;
ELSE
L_MULTIPLE := 1;
END IF;
L_SUM := NVL(L_SUM, 0) + SUBSTR(V_CODIGO_EAN13, I, 1) * L_MULTIPLE;
END LOOP;
RETURN V_CODIGO_EAN13||REPLACE((10 - MOD(L_SUM, 10)),10,0);
ELSE
RETURN NULL;
END IF;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment