Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Validating VAT Numbers

Author  Topic 

dnf999
Constraint Violating Yak Guru

253 Posts

Posted - 2006-12-11 : 07:01:51
Hi

Would anybody have any T-SQL script that validates UK (and european) VAT numbers to ensure they are the correct format? and I don't just mean field lengths but the actual validity of the numbering structure in the VAT number?

Thanks in advance!

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-11 : 07:18:28
If you can define what VAT number formats do you exactly want, you can get some ideas here.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2006-12-11 : 07:23:07
you should also validate their "format" in the front end application. Do not allow garbage to get to the database. I understand you can also put this check in the DB to reject crap too.

[Signature]For fast help, follow this link:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx
Learn SQL
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-11 : 07:36:36
[code]CREATE FUNCTION dbo.fnValidateVAT
(
@VAT VARCHAR(8000)
)
RETURNS BIT
AS

BEGIN
IF @VAT LIKE '%[^0-9]%'
RETURN 0

DECLARE @Index SMALLINT,
@Multiplier TINYINT,
@Sum INT,
@Plus TINYINT

SELECT @Index = LEN(@VAT),
@Multiplier = 1,
@Sum = 0

WHILE @Index >= 1
SELECT @Plus = @Multiplier * CAST(SUBSTRING(@VAT, @Index, 1) AS TINYINT),
@Multiplier = 3 - @Multiplier,
@Sum = @Sum + @Plus / 10,
@Sum = @Sum + @Plus % 10,
@Index = @Index - 1

RETURN CASE WHEN @Sum % 10 = 0 THEN 1 ELSE 0 END
END[/code]
Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-11 : 07:45:59
This function is handy for all MOD10 checks (aka Luhn algorithm).
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76195


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -