Validar CUIT/CUIL para Argentina desde una función en Microsoft SQL Server – Validate Argentinian CUIT/CUIL from Microsoft SQL Server function


Si necesitas una función en MSSQL para validar un número de CUIT o CUIL, necesitas el siguiente código (si la necesitas en otro lenguaje o base de datos busca en este link):

If you need a function in MSSQL to validate an Argentinian CUIT or CUIL number, you need the following code (if you need it in another language or database look at this link):

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) 
	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

About the author:

Matías Creimerman

Matías Creimerman
I’m a specialist in design, development and management of software solutions with almost 20 years of experience. Microsoft Certificated Professional (MCP). Expert in dot net and Microsoft technologies. Experience and skills in designing solutions in a wide range of commercial, industrial and production areas. Design of architectures, software applications and processes. Skills in leadership and team management. Tech trainer. Technology researcher. Self-taught and dedicated to continuous learning. Skills in estimation, quotation, projects proposals and solutions design. Entrepreneurial spirit. Strong Tech profile but also customer oriented. I perform roles as fullstack dev, tech consultant, technical referent, development leader, team leader, architect, cross leader, tech manager, tech director, trainer, ramp-up & follow-up teams, software factory manager, DevOps and release manager. Regular chess player and musician.

Professional Website

In

Blogger

Github

About Me

Portfolio

Wordpress - Arquitectura y desarrollo de software

Wordpress - Personal Blog

Microsoft - Youracclaim Badges

Microsoft - Tech Profile

Microsoft - ASP.NET Forum

tw
Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
Creative Commons License
This content is property of Matias Creimerman Any misuse of this material will be punishable
This work is licensed under a International Copyright Law protects «original works of authorship»
including photographs, videos, and blog posts posted on social media sites
The content has no rights to be shared without authorization or citation to the author.
This content cannot be sold be adapted or modified partially or totally.
All content shared outside this blog that doesn’t belong to the author must have citations to the author.

Publicado por:

Matias Creimerman

I’m a specialist in design, development and management of software solutions with 25 years of experience. Microsoft Certificated Professional (MCP). Expert in dot net and Microsoft technologies. Experience and skills in designing solutions in a wide range of commercial, industrial and production areas. Design of architectures, software applications and processes. Skills in leadership and team management. Tech trainer. Technology researcher. Self-taught and dedicated to continuous learning. Skills in estimation, quotation, projects proposals and solutions design. Entrepreneurial spirit. Strong Tech profile but also customer oriented. I perform roles as fullstack dev, tech consultant, technical referent, development leader, team leader, architect, cross leader, tech manager, tech director, trainer, ramp-up & follow-up teams, software factory manager, DevOps and release manager. Regular chess player and musician.

Categorías CódigoEtiquetas , , , , , , , , , , , , , , , , , , , 3 comentarios

3 comentarios en “Validar CUIT/CUIL para Argentina desde una función en Microsoft SQL Server – Validate Argentinian CUIT/CUIL from Microsoft SQL Server function”

Deja un comentario