Función para calcular Digito Verificador en Sql Server

La siguiente función permite calcular el digito verificador de un rut en SQL Server

CREATE  FUNCTION [dbo].[ObtenerDigitoVerificador]
(
	@rut INTEGER
 )
 RETURNS VARCHAR(1)

 AS
 BEGIN

 DECLARE @dv VARCHAR(1)
 DECLARE @rutAux INTEGER
 DECLARE @Digito INTEGER
 DECLARE @Contador INTEGER
 DECLARE @Multiplo INTEGER
 DECLARE @Acumulador INTEGER


 SET @Contador = 2;
 SET @Acumulador = 0;
 SET @Multiplo = 0;

	WHILE(@rut!=0)
		BEGIN

			SET @Multiplo = (@rut % 10) * @Contador;
			SET @Acumulador = @Acumulador + @Multiplo;
			SET @rut = @rut / 10;
			SET @Contador = @Contador + 1;
			if(@Contador = 8)
			BEGIN
				SET @Contador = 2;
			End;
		END;

	SET @Digito = 11 - (@Acumulador % 11);

	SET @dv = LTRIM(RTRIM(CONVERT(VARCHAR(2),@Digito)));

	IF(@Digito = 10)
	BEGIN
		SET @dv = 'K';
	END;

	IF(@Digito = 11)
	BEGIN
		SET @dv = '0';
	END;

RETURN @dv

END

GO

Espero sea de su utilidad

Saludos

También te podría gustar...

22 Respuestas

  1. Luis dice:

    Excelente Colaboración!! Muy Agradecido!

  2. NELSON CARDENAS dice:

    esta si sale bien

    — ==========================================================================================
    — Author: Nelson Cardenas
    — Create date: 26/09/2018
    — Description: Función para calcular digito de verificación de persona juridica
    — ==========================================================================================
    ALTER FUNCTION [SIS].[CalcularDigitoVerificacion]
    (
    @Documento BIGINT
    )
    RETURNS INT
    AS
    BEGIN
    IF(@Documento IS NULL)
    RETURN NULL

    DECLARE
    @Result INT = NULL
    ,@x INT = 0
    ,@y INT = 0
    ,@i INT = 0
    ,@z INT = LEN(@Documento)

    DECLARE @Specification TABLE(
    ID INT,
    Valor INT
    )
    INSERT INTO @Specification(
    ID
    ,Valor
    )
    VALUES
    (1, 3),
    (2, 7),
    (3, 13),
    (4, 17),
    (5, 19),
    (6, 23),
    (7, 29),
    (8, 37),
    (9, 41),
    (10, 43),
    (11, 47),
    (12, 53),
    (13, 59),
    (14, 67),
    (15, 71)

    WHILE (@i 1)
    SET @Result = 11 – @y
    ELSE
    SET @Result = @y

    RETURN @Result

    END

  3. Elen dice:

    muchas gracias por tu tiempo y colaboracion en compartir el codigo!

    saludos desde Santiago 🙂

  4. Root dice:

    FUNCION PARA CLACULAR LA EDAD DE UNA PERSONA
    DELIMITER //

    CREATE FUNCTION edad(fecha_nac DATE) RETURNS INT
    BEGIN
    DECLARE anio_actual INT;
    DECLARE anio_nac INT;
    DECLARE mes_actual INT;
    DECLARE mes_nac INT;
    DECLARE dia_actual INT;
    DECLARE dia_nac INT;
    DECLARE fecha_hoy DATE;
    DECLARE anios INT;

    SET fecha_hoy = current_date();
    SET anios = YEAR(fecha_hoy)- YEAR(fecha_nac);
    SET mes_actual = MONTH(fecha_hoy);
    SET mes_nac = MONTH(fecha_nac);
    SET dia_actual = DAY(fecha_hoy);
    SET dia_nac = DAY(fecha_nac);

    IF((mes_actual<mes_nac) OR
    (mes_actual=mes_nac AND dia_actual<dia_nac)) THEN
    SET anios= anios – 1;
    END IF;
    RETURN anios;
    END;
    //

    CREO LA TABLA PARA INTRODUCIR DATOS

    CREATE TABLE persona
    (persona_id INT AUTO_INCREMENT,
    fechanacimiento DATE,
    nombre VARCHAR (100),
    lugardenacimiento VARCHAR (100),
    CONSTRAINT pkpersona PRIMARY KEY (persona_id)
    );

    INSERT INTO persona (nombre, fechanacimiento, lugardenacimiento)
    VALUES ('Maria','1996-09-11','Oaxaca'),
    ('Pedro','1996-12-27','Oaxaca'),
    ('Juanito','1996-12-15','Oaxaca'),
    ('Meliton','1996-09-22','Oaxaca'),
    ('Hector','1996-05-24','Oaxaca');

    PARA HACER LA CONSULTA
    SELECT nombre, edad(fechanacimiento) AS edad FROM persona;

  5. jorge dice:

    Agrego la última modificación (por ahora). Debido a que en algunos casos, el dígito verificador da 10, esta rutina lo que hace es poner una K como verificador en esos casos, cuando en realidad debería iterar nuevamente cambiando el prefijo (de 20 o 27 según sea el caso) a 23.

    Agrego la modificación correspondiente.

    /****** Object: UserDefinedFunction [dbo].[GeneraCUIT] Script ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE FUNCTION [dbo].[GeneraCUIT]
    (
    @pre integer,
    @rut bigint
    )
    RETURNS VARCHAR(15)

    AS
    BEGIN

    DECLARE @DNI VARCHAR(8)
    DECLARE @dv VARCHAR(1)
    DECLARE @rutAux INTEGER
    DECLARE @Digito INTEGER
    DECLARE @Contador INTEGER
    DECLARE @Multiplo INTEGER
    DECLARE @Acumulador INTEGER
    DECLARE @ret VARCHAR(13)

    SET @Contador = 2;
    set @dni=cast(@rut as varchar);
    SET @Acumulador = 0;
    SET @Multiplo = 0;
    set @rut=@pre*1e8+@rut;
    While Len(@rut) < 8
    begin
    set @dni= '0' + @dni
    end;

    WHILE(@rut!=0)
    BEGIN

    SET @Multiplo = (@rut % 10) * @Contador;
    SET @Acumulador = @Acumulador + @Multiplo;
    SET @rut = @rut / 10;
    SET @Contador = @Contador + 1;
    if(@Contador = 8)
    BEGIN
    SET @Contador = 2;
    End;
    END;

    SET @Digito = 11 – (@Acumulador % 11);

    SET @dv = LTRIM(RTRIM(CONVERT(VARCHAR(2),@Digito)));

    IF(@Digito = 10)
    BEGIN
    SET @pre = '23';
    set @Acumulador=0;
    set @Contador=2;
    set @rut=cast(@pre as int)*1e8 +cast(@dni as bigint);

    WHILE(@rut!=0)
    BEGIN

    SET @Multiplo = (@rut % 10) * @Contador;
    SET @Acumulador = @Acumulador + @Multiplo;
    SET @rut = @rut / 10;
    SET @Contador = @Contador + 1;
    if(@Contador = 8)
    BEGIN
    SET @Contador = 2;
    End;
    END;

    SET @Digito = 11 – (@Acumulador % 11);

    SET @dv = LTRIM(RTRIM(CONVERT(VARCHAR(2),@Digito)));

    END;

    IF(@Digito = 11)
    BEGIN
    SET @dv = '0';
    END;

    SET @ret = cast(@pre as varchar)+'-'+cast(@dni as varchar)+'-'+@dv

    RETURN @ret

    END

    GO

    LLamo ala funcion desde el SQL como
    select dbo.GeneraCUIT(20,012345678), dbo.GeneraCUIT(27,012345678)

    en el caso de que tenga en la tabla un campo con el sexo y otro con DNI, puedo hacer una seleccion condicional.

    p.ej.

    Select distinct sexo, dni,
    case when sexo=1 then datos.dbo.GeneraCUIT(20,DNI) else
    datos.dbo.GeneraCUIT(27,DNI) end as cuit
    from tabla1

    En este caso, el campo sexo tiene un 1 si es hombre y un 2 si es mujer…. no hay lugar para otras variantes.

    Si fuese un campo texto con una 'M' o 'H', deberán hacerse las modificiaciones en el sql correspondientes

    Select distinct sexo, dni,
    case when sexo='M' then datos.dbo.GeneraCUIT(20,DNI) else
    datos.dbo.GeneraCUIT(27,DNI) end as cuit
    from tabla1

    Saludos!

    PD. Muchas gracias al generador del Código Original (Victor Riquelme)

    Jorge

    • Estimado disculpa la ignorancia pero el CUIT, para que es utilizado? vi que era un identificador, pero tambien lei que en el caso de chile correspondia al RUT. Podrias explicarme en breve que formato y utilidad tiene.

      Saludos

  6. jorge dice:

    Agrego unas modificaciones para generar el cuit a partir de un campo de prefijo (pre). Si es Hombre (20) Mujer (27) sociedad/empresa (30).

    Luego llaman a la funcion como dbo.GenerarCuit(pre,dni)
    y da como resultado el CUIT correspondiente.

    Saludos y muchas Gracias.

    CREATE FUNCTION [dbo].[GenerarCUIT]
    (
    @pre integer,
    @rut bigint
    )
    RETURNS VARCHAR(15)

    AS
    BEGIN

    DECLARE @DNI VARCHAR(8)
    DECLARE @dv VARCHAR(1)
    DECLARE @rutAux INTEGER
    DECLARE @Digito INTEGER
    DECLARE @Contador INTEGER
    DECLARE @Multiplo INTEGER
    DECLARE @Acumulador INTEGER
    DECLARE @ret VARCHAR(13)

    SET @Contador = 2;
    set @dni=cast(@rut as varchar);
    SET @Acumulador = 0;
    SET @Multiplo = 0;
    set @rut=@pre*1e8+@rut;

    WHILE(@rut!=0)
    BEGIN

    SET @Multiplo = (@rut % 10) * @Contador;
    SET @Acumulador = @Acumulador + @Multiplo;
    SET @rut = @rut / 10;
    SET @Contador = @Contador + 1;
    if(@Contador = 8)
    BEGIN
    SET @Contador = 2;
    End;
    END;

    SET @Digito = 11 – (@Acumulador % 11);

    SET @dv = LTRIM(RTRIM(CONVERT(VARCHAR(2),@Digito)));

    IF(@Digito = 10)
    BEGIN
    SET @dv = ‘K’;
    END;

    IF(@Digito = 11)
    BEGIN
    SET @dv = ‘0’;
    END;

    SET @ret = cast(@pre as varchar)+’-‘+cast(@dni as varchar)+’-‘+@dv

    RETURN @ret

    END

  7. Marcos dice:

    Pueden pasar por favor el codigo para POSTGRESQL

  8. En PHP

    <?php

    /**
    * @author Doglas A. Dembogurski Feix
    */

    class DigitoVerificador{

    function __construct(){ }

    function calcularDV($numero){
    $basemax = 11;
    $codigo;
    $numero_al = "";
    $caracter;//extrae el caracter!!!
    for($i = 0; $i = 0; $i–) {
    if($k > $basemax) {
    $k = 2;
    }
    $aux = $numero_al{$i};
    $v_total = $v_total + ($aux * $k);
    $k = $k + 1;
    }
    $resto = $v_total % 11;
    $dv = 0;
    if($resto > 1){
    $dv = 11 – $resto;
    }
    return $dv;
    }
    }

    $d = new DigitoVerificador();
    $dv = $d->calcularDV(«80001404»);
    echo «El digito Verificador de 80001404 es: $dv» ;

    ?>

  9. dario dice:

    que significa «%» en esta linea ..?? (rut%10)

  10. Totoro dice:

    En MySQL:

    CREATE FUNCTION `ObtenerDV`(`rut` INT UNSIGNED) RETURNS varchar(1)

    BEGIN
    DECLARE dv VARCHAR(1);
    DECLARE rutAux INT;
    DECLARE Digito INT;
    DECLARE Contador INT;
    DECLARE Multiplo INT;
    DECLARE Acumulador INT;

    SET Contador = 2;
    SET Acumulador = 0;
    SET Multiplo = 0;

    WHILE(rut!=0) DO
    SET Multiplo = (rut%10) * Contador;
    SET Acumulador = Acumulador + Multiplo;
    SET rut = FLOOR(rut / 10);
    SET Contador = Contador + 1;
    if(Contador = 8) THEN
    SET Contador = 2;
    END IF;
    END WHILE;

    SET Digito = 11 – (Acumulador%11);

    SET dv = LTRIM(RTRIM(CAST(Digito as CHAR(2))));

    IF(Digito = 10) THEN
    SET dv = ‘K’;
    END IF;

    IF(Digito = 11) THEN
    SET dv = ‘0’;
    END IF;

    RETURN dv;
    END;

  11. mmmsa dice:

    no calcula bn

  12. eladio azua dice:

    te pasaste perrin, muchas gracias

Deja una respuesta

Tu dirección de correo electrónico no será publicada.