## mod 10 weights 1, 2

 Author Message NEAL-464478 SSC-Enthusiastic Group: General Forum Members Points: 157 Visits: 158 It is a MOD-10 summation digits based on weights of 1,2........ now i would like achive that within SQL 2000 ..... is it possible with... i am new to sql 2000, so please bare with mef1 i have input data and f2 i like to show the calcuated result.' Note the following source code is designed to calculate a check digit for:' a Mod 10 Sum of Values check digit routine that uses the 1, 2 weighting.' Characters are read from left to right in performing the calculation.Public Function DetermineCheckDigit(ByVal Sequence As String) As Integer Dim intAscVal As Integer Dim intCalcCheckDigit As Integer Dim intI As Integer Dim intLen As Integer Dim intRmndr As Integer Dim intSum As Integer Dim intTemp1 As Integer Dim intValues() As Integer Dim intWeight As Integer 'Calculate numeric intValues of string characters intLen = Len(Sequence) ReDim intValues(intLen) For intI = 1 To intLen intAscVal = Asc(Mid\$(Sequence, intI, 1)) 'If numeric, keep as is If (intAscVal >= 48 And intAscVal <= 57) Then intValues(intI) = intAscVal - 48 'If non-numeric, use replacement value ElseIf (intAscVal >= 65 And intAscVal <= 90) Then Select Case Chr\$(intAscVal) Case "A", "K", "U" intValues(intI) = 0 Case "B", "L", "V" intValues(intI) = 1 Case "C", "M", "W" intValues(intI) = 2 Case "D", "N", "X" intValues(intI) = 3 Case "E", "O", "Y" intValues(intI) = 4 Case "F", "P", "Z" intValues(intI) = 5 Case "G", "Q" intValues(intI) = 6 Case "H", "R" intValues(intI) = 7 Case "I", "S" intValues(intI) = 8 Case "J", "T" intValues(intI) = 9 Case Else intValues(intI) = 0 End Select Else intValues(intI) = 0 End If Next intI 'Start weighting each value with using Mod-10 weights (1-2) intWeight = 1 intSum = 0 For intI = 1 To intLen intTemp1 = (intWeight * intValues(intI)) intSum = intSum + intTemp1 intWeight = intWeight + 1 If (intWeight > 2) Then intWeight = 1 Next intI 'Drop all but last digit intRmndr = (intSum Mod 10) If (intRmndr = 0) Then intRmndr = 10 End If intCalcCheckDigit = 10 - intRmndr DetermineCheckDigit = intCalcCheckDigitEnd Function Ray K SSChampion Group: General Forum Members Points: 13668 Visits: 4955 I don't think you're going to find too many people here who are going to be able to (or even want) to go through your VB code.Better: can you give us examples of the data (not the code) input and output you're looking for? And please keep it simple, short, and sweet.Thanks! +--------------------------------------------------------------------------------------+‌Check out my blog at https://pianorayk.wordpress.com/ Lowell SSC Guru Group: General Forum Members Points: 182339 Visits: 41569 i think you are looking for the LUHN mod 10 credit card validation, right? googling "TSQL LUHN mod 10"got me this tsql code snippet"http://www.novicksoftware.com/UDFofWeek/Vol2/T-SQL-UDF-Vol-2-Num-47-udf_Bank_IsLuhn.htm Lowell--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible! Lynn Pettis SSC Guru Group: General Forum Members Points: 223132 Visits: 40398 Assuming you have a Tally table, how about the following code:`declare @ TestData varchar(12);set @TestData = '03EA12J77';select (10 - (sum( (((N + 1) % 2) + 1) * case when substring(@ TestData, N, 1) in ('0','A','K','U') then 0 when substring(@ TestData, N, 1) in ('1','B','L','V') then 1 when substring(@ TestData, N, 1) in ('2','C','M','W') then 2 when substring(@ TestData, N, 1) in ('3','D','N','X') then 3 when substring(@ TestData, N, 1) in ('4','E','O','Y') then 4 when substring(@ TestData, N, 1) in ('5','F','B','Z') then 5 when substring(@ TestData, N, 1) in ('6','G','Q') then 6 when substring(@ TestData, N, 1) in ('7','H','R') then 7 when substring(@ TestData, N, 1) in ('8','I','S') then 8 when substring(@ TestData, N, 1) in ('9','J','T') then 9 else 0 end ) % 10)) % 10from dbo.Tallywhere N <= len(@TestData);`You will need to remove the space I had to enter between @ sign and the T in the variable name to get this to post.If you don't have a Tally table, here is a link to an article about them:http://www.sqlservercentral.com/articles/T-SQL/62867/. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) NEAL-464478 SSC-Enthusiastic Group: General Forum Members Points: 157 Visits: 158 Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.Server: Msg 8116, Level 16, State 1, Line 3Argument data type nvarchar is invalid for argument 2 of substring function.showing me above error in query analyzer i posted folowing code declare @TestData varchar(12);set @TestData = '03EA12J77';select (10 - (sum( (((N + 1) % 2) + 1) * case when substring(@TestData, N, 1) in ('0','A','K','U') then 0 when substring(@TestData, N, 1) in ('1','B','L','V') then 1 when substring(@TestData, N, 1) in ('2','C','M','W') then 2 when substring(@TestData, N, 1) in ('3','D','N','X') then 3 when substring(@TestData, N, 1) in ('4','E','O','Y') then 4 when substring(@TestData, N, 1) in ('5','F','B','Z') then 5 when substring(@TestData, N, 1) in ('6','G','Q') then 6 when substring(@TestData, N, 1) in ('7','H','R') then 7 when substring(@TestData, N, 1) in ('8','I','S') then 8 when substring(@TestData, N, 1) in ('9','J','T') then 9 else 0 end ) % 10)) % 10from dbo.Tallywhere N <= len(@TestData); Lynn Pettis SSC Guru Group: General Forum Members Points: 223132 Visits: 40398 N is the column name in my Tally table (dbo.Tally). Do you have a Tally table? If so, what is the name of the column in your Tally table? You will need to replace the N in my query with that column name.If you don't have a Tally table, you need to create one. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Jeff Moden SSC Guru Group: General Forum Members Points: 505594 Visits: 44254 Lynn Pettis (1/7/2010)Assuming you have a Tally table, how about the following code:`declare @ TestData varchar(12);set @TestData = '03EA12J77';select (10 - (sum( (((N + 1) % 2) + 1) * case when substring(@ TestData, N, 1) in ('0','A','K','U') then 0 when substring(@ TestData, N, 1) in ('1','B','L','V') then 1 when substring(@ TestData, N, 1) in ('2','C','M','W') then 2 when substring(@ TestData, N, 1) in ('3','D','N','X') then 3 when substring(@ TestData, N, 1) in ('4','E','O','Y') then 4 when substring(@ TestData, N, 1) in ('5','F','B','Z') then 5 when substring(@ TestData, N, 1) in ('6','G','Q') then 6 when substring(@ TestData, N, 1) in ('7','H','R') then 7 when substring(@ TestData, N, 1) in ('8','I','S') then 8 when substring(@ TestData, N, 1) in ('9','J','T') then 9 else 0 end ) % 10)) % 10from dbo.Tallywhere N <= len(@TestData);`You will need to remove the space I had to enter between @ sign and the T in the variable name to get this to post.If you don't have a Tally table, here is a link to an article about them: http://www.sqlservercentral.com/articles/T-SQL/62867/.Just curious... why are you substacting the mod 10 checksum from 10?Also, the bold enhancement on your link broke the link. --Jeff ModenRBAR 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.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC Guru Group: General Forum Members Points: 505594 Visits: 44254 I don't know which will be faster nor which you prefer to read nor am I sure which is correct (I don't subtract my result from 10 like Lynn did... I do it like they do for Luhn Mod 10 Check Sums for Credit Cards).The other thing to remember is that if you use such a thing on credit cards, the right-most digit will always be treated as a "1" multiplier and then you work to the left in 1-2 order...`DECLARE @TestData VARCHAR(12), @ControlString CHAR(36) SELECT @TestData = '03EA12J78', @ControlString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' SELECT SUM(((N+1)%2+1)*(CHARINDEX(SUBSTRING(@TestData, N, 1),@ControlString)-1))%10 FROM dbo.Tally WHERE N <= LEN(@TestData)` --Jeff ModenRBAR 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.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC Guru Group: General Forum Members Points: 505594 Visits: 44254 Never mind, Lynn... I see why you're subtracting from 10... I did a validation of a full number and you're calculating the check digit.Modification of my code will change it from validation to calculating the check digit...`DECLARE @TestData VARCHAR(12), @ControlString CHAR(36) SELECT @TestData = '03EA12J78', @ControlString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' SELECT 10-(SUM(((N+1)%2+1)*(CHARINDEX(SUBSTRING(@TestData, N, 1),@ControlString)-1))%10) FROM dbo.Tally WHERE N <= LEN(@TestData)` --Jeff ModenRBAR 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.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Lynn Pettis SSC Guru Group: General Forum Members Points: 223132 Visits: 40398