You made the mistake of using ISNUMERIC to check for digits. ISNUMERIC does not equal ISALLDIGITS... try this and see what I mean...
Rather than just have it return a check digit, why not have it validate the ABA routing number altogether? For example...
[font="Courier New"]DECLARE @RoutingNumber VARCHAR(8000) --Allows check of wrong size numbers
SET @RoutingNumber = '067803457'
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 http://www.sqlservercentral.com/scripts/Advanced+SQL/62486/
WHERE t.N <= 8
ELSE 'Not Valid'
is pronounced "ree-bar
" and is a "Modenism
" for R
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.
"Change is inevitable... change for the better is not".
"If "pre-optimization" is the root of all evil, then what does the resulting no optimization lead to?"
How to post code problems
How to Post Performance Problems
Create a Tally Function (fnTally)