Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
SQL Server Newbies
»
Generate a checkletter (code)
Generate a checkletter (code)
Rate Topic
Display Mode
Topic Options
Author
Message
a_ud
a_ud
Posted Tuesday, August 21, 2012 2:41 AM
Valued Member
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 10:55 AM
Points: 67,
Visits: 265
I'm trying to translate a stored procedure written in SQL to do the same in VBA-Access. Code is attached. Although I mostly get what it does (for a number like 1234, generate a letter), some details are missing.
Could you describe in letter the flowgram (specially command in italics-bold) for '1234'? What happens in iterations higher than 5 for this number (1234)? Thanks in advance, a.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[CheckLetter] ( @ID INT )
RETURNS VARCHAR
AS BEGIN
DECLARE @Result VARCHAR
DECLARE @ABC VARCHAR(23)
DECLARE @Mod INT
DECLARE @I INT
DECLARE @Num INT
SET @Mod = 0
SET @I = 1
WHILE ( @I < 7 )
BEGIN
SET @Mod = @Mod + ( @I
* CONVERT(INT,
SUBSTRING(CONVERT(VARCHAR, @ID),
@I, 1))
)
SET @I = @I + 1
END
SET @Mod = @Mod % 23
SET @ABC = 'ABCDEFGHJKLMNPQRSTVWXYZ'
-- Add the T-SQL statements to compute the return value here
SELECT @Result = SUBSTRING(@ABC, @Mod + 1, 1)
-- Return the result of the function
RETURN @Result
END
Post #1347637
Sean Lange
Sean Lange
Posted Tuesday, August 21, 2012 7:49 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 8,547,
Visits: 8,204
Not really sure what your question is but you can read about substring here.
http://msdn.microsoft.com/en-us/library/ms187748.aspx
What are the business rules for this? This looks pretty strange.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1347799
a_ud
a_ud
Posted Tuesday, August 21, 2012 10:08 AM
Valued Member
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 10:55 AM
Points: 67,
Visits: 265
Thanks Sean. By now I've got a very clear idea of what the (mod 23) code does in SQL (code in VBA still pending).
Basically it takes a number 'ID' like '1234' and calculates the following in each iteration 'i':
i | mod | mod + i*substring(id,i1)
1 | 0 0+1 *1=1
2 | 1 | 1+2*2 = 5
.........
7 | .... | 30
Then 30 mod 23 = 7 --> and 7+1 would be the value used for looking up the corresponding letter, an H ("ABCDEFGHJKLMNPQRSTVWXYZ"). This can be translated into VBA relatively easily, since Substring is equivalent to Excel function Mid.
Hope this helps someone, a.
Post #1347910
Sean Lange
Sean Lange
Posted Tuesday, August 21, 2012 10:16 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 8,547,
Visits: 8,204
a_ud (8/21/2012)
Thanks Sean. By now I've got a very clear idea of what the (mod 23) code does in SQL (code in VBA still pending).
Basically it takes a number 'ID' like '1234' and calculates the following in each iteration 'i':
i | mod | mod + i*substring(id,i1)
1 | 0 0+1 *1=1
2 | 1 | 1+2*2 = 5
.........
7 | .... | 30
Then 30 mod 23 = 7 --> and 7+1 would be the value used for looking up the corresponding letter, an H ("ABCDEFGHJKLMNPQRSTVWXYZ"). This can be translated into VBA relatively easily, since Substring is equivalent to Excel function Mid.
Hope this helps someone, a.
I can understand the sql part but what I can't understand is what the business rule for this is. It is a very strange algorithm to pick a letter. I am curious what this is used for and the logic is the way it is.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1347915
a_ud
a_ud
Posted Tuesday, August 21, 2012 10:33 AM
Valued Member
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 10:55 AM
Points: 67,
Visits: 265
It's quite standard (at least in Europe!), it's basically a check letter that you generate for verifying a sequence of numbers (like the control digit in a check, a barcode, or a letter for an ID card).
Once you've got the final mod and letter (for '1234' was H), you'd add that to the sequence of nos. So the full check-ID number would read: 1234-H.
Some devices like barcode readers might have code checking that the letter matches the nos, that's all...
Post #1347930
Sean Lange
Sean Lange
Posted Tuesday, August 21, 2012 10:39 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 8,547,
Visits: 8,204
a_ud (8/21/2012)
It's quite standard (at least in Europe!), it's basically a check letter that you generate for verifying a sequence of numbers (like the control digit in a check, a barcode, or a letter for an ID card).
Once you've got the final mod and letter (for '1234' was H), you'd add that to the sequence of nos. So the full check-ID number would read: 1234-H.
Some devices like barcode readers might have code checking that the letter matches the nos, that's all...
Ahh I gotcha. Makes sense now.
The code itself was easy enough to decipher I just couldn't figure out what the point was.
_______________________________________________________________
Need help? Help us help you.
Read the article at
http://www.sqlservercentral.com/articles/Best+Practices/61537/
for best practices on asking questions.
Need to split a string? Try Jeff Moden's
splitter
.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1347936
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.