BACK
TOP
Validate Argentinian CUIT-CUIL from SQL Server function
Matías Creimerman - Buenos Aires, Argentina - 2014-11-07
IF EXISTS (
SELECT
*
FROM
sys.objects
WHERE
object_id = OBJECT_ID(N'[dbo].[fn_ValidarCUIT]’) AND type in (N’FN’, N’IF’, N’TF’, N’FS’, N’FT’)
)
begin
drop function fn_ValidarCUIT
end
GO

create function fn_ValidarCUIT (@cuit varchar(13))
returns  bit
as

begin
declare @verificador int
declare @resultado int = 0
declare @cuit_nro varchar(11)
declare @validacion bit
declare @codes varchar(10) = ‘6789456789’

set @cuit_nro = REPLACE(@cuit, ‘-‘, ”)

if isnumeric(@cuit_nro) 1
begin
return 0
end

if len(@cuit_nro) 11
begin
set @validacion = 0
end

set @verificador = RIGHT(@cuit_nro, 1)

declare @x int = 0

while @x < 10
begin
declare @digitoValidador int = convert(int, substring(@codes, @x + 1, 1))
declare @digito int = convert(int, substring(@cuit_nro, @x + 1, 1))
declare @digitoValidacion int = @digitoValidador * @digito
set @resultado = @resultado + @digitoValidacion
set @x = @x + 1
end

set @resultado = @resultado % 11

If @resultado = @verificador
begin
set @validacion = 1
end
else
begin
set @validacion = 0
End

return @validacion
end
This content is property of Matias Creimerman
Any misuse of this material will be punishable
This work is licensed under a
Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
Creative Commons License