I'm looking SQL command for the MOD10 Validation on 23rd numeric number. We have all 22 numbers are numeric from that I need to get the 23rd number as MOD10 check. Please feel free to share with me if any of you have the logic.
I have posted a plain TSQL routine here to do just that few months ago. Let me try to find it and I will post it.
Jun 28, 2011 at 02:32 PM
Oleg
(comments are locked)

Here it goes:
Just restate it as you see fit. The number provided in the sample results in the valid combination compliant with CC10 a.k.a. MOD10 algorithm. In your case, you probably want to consider passing the 22 digits and one more zero on the right of it to the guts of the select statement, which returns the sum. Take 10  (mod 10 of it) and you will get the value of the rightmost digit as it should be in place of zero. <! Begin Edit Here is the inline scalar function calculating the value of the control digit. For example, if your final number has to be CC10 compliant 23 digits number and you already know your 22 digits input then pass these 22 digits as a parameter to the function and it will return the value of the control digit. The latter can then be used to append to the right of the original input. You can use less digits if you need to but not more. A good way to check the validity of the function below is you pass any of your existing credit card numbers (except the last digit) as a parameter to it and the function will "reveal" the value of the last digit on your card. Since the function operates with assumption that the input does not include the control number (of course), the logic to pick the numbers to interrogate by the algorithm steps is reversed from the one in the original validation query above, the one which did need the control number to be already included and simply stated whether the whole bunch is compliant or not. Here is the function definition:
Of course in reality it would be far better to maintain a tiny Tally table instead of interrogating the sys.objects every time the function is called. For example,
End Edit > <! Begin Edit 2 Here is the version which will work for SQL Server 2000. This is reduced to work only with 22 character inputs because I don't remember whether SQL Server 2000 lets you pass a variable into select top (@n) or not. If it does though then please replace the select top 22 part with the original select top (len(@input)). Here is the script:
The above assumes that TinyTally table already exists and has 23 (or more) records in it. End Edit 2 > OlegThanks for you reply Oleg. I belive my question was not straight. Based on my inpute value 22 digit I need to make 23rd digit as MOD10 check value.
Jun 29, 2011 at 07:54 AM
SQLFAQ
@SQLFAQ I understand. This is why I have a comment on the bottom of my answer explaining how to calculate it. Please let me know whether you need a procedure returning the control number value or a scalar function returning the same and I will add it to my answer.
Jun 29, 2011 at 08:18 AM
Oleg
I may need to return this result as value and concatenate into other value so I think the scalar funciton will do good for my requirement. Thanks for your quick response.
Jun 29, 2011 at 08:50 AM
SQLFAQ
@SQLFAQ I updated my answer. Please let me know if the function does what you need it to do.
Jun 29, 2011 at 09:37 AM
Oleg
Thanks for your effort Oleg. I will test this logic and let you know the result some time next week. I hope this script will work fine but still I will confirm the result.
Jun 29, 2011 at 01:54 PM
SQLFAQ
(comments are locked)
