January 7, 2010 at 1:02 pm
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
January 7, 2010 at 1:24 pm
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/
January 7, 2010 at 1:50 pm
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
January 7, 2010 at 3:19 pm
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:
January 7, 2010 at 4:31 pm
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);
January 7, 2010 at 5:55 pm
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.
January 7, 2010 at 8:45 pm
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
Change is inevitable... Change for the better is not.
January 7, 2010 at 9:04 pm
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
Change is inevitable... Change for the better is not.
January 7, 2010 at 9:09 pm
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
Change is inevitable... Change for the better is not.
January 7, 2010 at 10:01 pm
Fixed the link in my previous post. Thanks Jeff.
January 8, 2010 at 9:08 am
Server: Msg 207, Level 16, State 3, Line 6
Invalid column name 'N'.
Server: Msg 207, Level 16, State 1, Line 6
Invalid column name 'N'.
Server: Msg 207, Level 16, State 1, Line 6
Invalid column name 'N'.
sschampion --i get above error when use your code on query analyzer, i am new to sql 2000 so please explain how should i implement this.
thanks a bunch for your help
January 8, 2010 at 9:25 am
Here is my table--
CREATE TABLE [dbo].[Tally] (
[TestData] [varchar] (12) ,
[ControlString] [varchar] (50) ,
[Nek] [varchar] (50)
) ON [PRIMARY]
declare @TestData varchar(12);
set @TestData = '03EA12J77';
select
(10 - (sum(
(((Nek + 1) % 2) + 1) *
case
when substring(@TestData, Nek, 1) in ('0','A','K','U') then 0
when substring(@TestData, Nek, 1) in ('1','B','L','V') then 1
when substring(@TestData, Nek, 1) in ('2','C','M','W') then 2
when substring(@TestData, Nek, 1) in ('3','D','N','X') then 3
when substring(@TestData, Nek, 1) in ('4','E','O','Y') then 4
when substring(@TestData, Nek, 1) in ('5','F','B','Z') then 5
when substring(@TestData, Nek, 1) in ('6','G','Q') then 6
when substring(@TestData, Nek, 1) in ('7','H','R') then 7
when substring(@TestData, Nek, 1) in ('8','I','S') then 8
when substring(@TestData, Nek, 1) in ('9','J','T') then 9
else 0
end
) % 10)) % 10
from
dbo.Tally
where
Nek <= len(@TestData);
i replaces N with Nek and i get folloowing error---
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
Server: Msg 8116, Level 16, State 1, Line 3
Argument data type varchar is invalid for argument 2 of substring function.
January 8, 2010 at 9:29 am
DECLARE @TestData VARCHAR(12),
@ControlString CHAR(36)
SELECT @TestData = '03EA12J78',
@ControlString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
SELECT 10-(SUM(((Nek+1)%2+1)*(CHARINDEX(SUBSTRING(@TestData, Nek, 1),@ControlString)-1))%10)
FROM dbo.Tally
WHERE Nek <= LEN(@TestData)
got following error---
Server: Msg 8116, Level 16, State 1, Line 6
Argument data type varchar is invalid for argument 2 of substring function.
January 8, 2010 at 9:36 am
Your Tally table isn't. Please read the 4th article I reference in my signature block below regarding Tally Tables. Trust me, it will be enlightening, it was for me.
June 25, 2012 at 12:07 pm
This is the exact coding I need. I am new to VBA so I am having a hard time putting all the code together with the tally table. Can you post the final version of the code in one post? Thanks in advance.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply