Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

How to Validate Bank Routing Using Check Digit Expand / Collapse
Posted Saturday, September 29, 2007 8:53 PM



Group: General Forum Members
Last Login: Thursday, April 10, 2014 9:33 AM
Points: 14, Visits: 22
Comments posted to this topic are about the item How to Validate Bank Routing Using Check Digit

Got SQL Server? Manage It with Siccolo
Post #404540
Posted Friday, April 11, 2008 11:26 PM



Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
You made the mistake of using ISNUMERIC to check for digits. ISNUMERIC does not equal ISALLDIGITS... try this and see what I mean...

SELECT dbo.check_digit('072---326')

Rather than just have it return a check digit, why not have it validate the ABA routing number altogether? For example...

DECLARE @RoutingNumber VARCHAR(8000) --Allows check of wrong size numbers
SET @RoutingNumber = '067803457'

WHEN LEN(@RoutingNumber)=9
AND @RoutingNumber NOT LIKE '%[^0-9]%' --Check to make sure is all digits
AND RIGHT(@RoutingNumber ,1)
= (--==== Calculates check digit (Digit 9)
SELECT 10-SUM(mv.DigitVal)%10 --Sum of individual digit values subtracted for new number ending in 0
FROM (--==== Gets multiplied value for each digit in 3, 7, 1 pattern
WHEN (t.N-1)%3 = 0 THEN SUBSTRING(@RoutingNumber,t.N,1)*3 --Digits 1,4,7
WHEN (t.N-1)%3 = 1 THEN SUBSTRING(@RoutingNumber,t.N,1)*7 --Digits 2,5,8
WHEN (t.N-1)%3 = 2 THEN SUBSTRING(@RoutingNumber,t.N,1)*1 --Digits 3,6
END AS DigitVal
FROM dbo.Tally t --See
WHERE t.N <= 8
) mv
THEN 'Valid'
ELSE 'Not Valid'

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #484035
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse