SQLを使用して「MOD10」チェックディジットアルゴリズムを実装しました。米国郵政公社の住所変更サービスのキーラインについては、ドキュメントの方法に従っていますが、間違った番号を取得しているようです。入力文字列には数字のみが含まれているため、計算が少し簡単になります。私の結果を彼らのテストアプリケーションの結果と比較すると、異なる数値が得られます。何が起こっているのか分かりませんか?誰かが私のアルゴリズムに何か問題があると思いますか?それは明白な何かでなければなりません...
このメソッドのドキュメントは、このドキュメントの12-13ページにあります 。http ://www.usps.com/cpim/ftp/pubs/pub8a.pdf
サンプルアプリケーションは次の場所にあります:http: //ribbs.usps.gov/acs/documents/tech_guides/KEYLINE.EXE
注意:フォーラムユーザーの助けを借りて、以下のコードを修正しました。これは、将来の読者がコード全体を使用できるようにするためです。
ALTER function [dbo].[udf_create_acs] (@MasterCustomerId varchar(26))
returns varchar(30)
as
begin
--this implements the "mod 10" check digit calculation
--for the US Postal Service ACS function, from "Publication 8A"
--found at "http://www.usps.com/cpim/ftp/pubs/pub8a.pdf"
declare @result varchar(30)
declare @current_char int
declare @char_positions_odd varchar(10)
declare @char_positions_even varchar(10)
declare @total_value int
declare @check_digit varchar(1)
--These strings represent the pre-calculated values of each character
--Example: '7' in an odd position in the input becomes 14, which is 1+4=5
-- so the '7' is in position 5 in the string - zero-indexed
set @char_positions_odd = '0516273849'
set @char_positions_even = '0123456789'
set @total_value = 0
set @current_char = 1
--stepping through the string one character at a time
while (@current_char <= len(@MasterCustomerId)) begin
--this is the calculation for the character's weighted value
if (@current_char % 2 = 0) begin
--it is an even position, so just add the digit's value
set @total_value = @total_value + convert(int, substring(@MasterCustomerId, @current_char, 1))
end else begin
--it is an odd position, so add the pre-calculated value for the digit
set @total_value = @total_value + (charindex(substring(@MasterCustomerId, @current_char, 1), @char_positions_odd) - 1)
end
set @current_char = @current_char + 1
end
--find the check digit (character) using the formula in the USPS document
set @check_digit = convert(varchar,(10 - (@total_value % 10)) % 10)
set @result = '#' + @MasterCustomerId + ' ' + @check_digit + '#'
return @result
end