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 12»»

mod 10 weights 1, 2 Expand / Collapse
Author
Message
Posted Thursday, January 7, 2010 1:02 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 1, 2014 11:46 PM
Points: 19, 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
Post #843831
Posted Thursday, January 7, 2010 1:24 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 20, 2014 7:01 AM
Points: 2,050, Visits: 3,525
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!
Post #843842
Posted Thursday, January 7, 2010 1:50 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:49 PM
Points: 12,922, Visits: 32,299
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #843860
Posted Thursday, January 7, 2010 3:19 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:25 AM
Points: 20,801, Visits: 32,725
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/.



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)
Post #843931
Posted Thursday, January 7, 2010 4:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, May 1, 2014 11:46 PM
Points: 19, 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);
Post #843983
Posted Thursday, January 7, 2010 5:55 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:25 AM
Points: 20,801, Visits: 32,725
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 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)
Post #844020
Posted Thursday, January 7, 2010 8:45 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:47 PM
Points: 35,556, Visits: 32,151
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."

(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 #844071
Posted Thursday, January 7, 2010 9:04 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:47 PM
Points: 35,556, Visits: 32,151
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."

(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 #844076
Posted Thursday, January 7, 2010 9:09 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:47 PM
Points: 35,556, Visits: 32,151
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."

(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 #844077
Posted Thursday, January 7, 2010 10:01 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:25 AM
Points: 20,801, Visits: 32,725
Fixed the link in my previous post. Thanks Jeff.



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)
Post #844082
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse