SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


mod 10 weights 1, 2


mod 10 weights 1, 2

Author
Message
NEAL-464478
NEAL-464478
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 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 me

f1 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 = intCalcCheckDigit

End Function
Ray K
Ray K
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6720 Visits: 4676
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
Lowell
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71510 Visits: 40930
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
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94595 Visits: 38956
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)) % 10
from
dbo.Tally
where
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/.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
NEAL-464478
NEAL-464478
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 158
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type nvarchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument 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)) % 10
from
dbo.Tally
where
N <= len(@TestData);
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94595 Visits: 38956
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214474 Visits: 41979
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)) % 10
from
dbo.Tally
where
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 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.
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 problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214474 Visits: 41979
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 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.
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 problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)SSC Guru (214K reputation)

Group: General Forum Members
Points: 214474 Visits: 41979
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 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.
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 problems
How to post performance problems
Forum FAQs
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94595 Visits: 38956
Fixed the link in my previous post. Thanks Jeff.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search