USPS ACS Keyline Check Digit
我已经使用SQL实现了“MOD 10”校验位算法,根据其文档中的方法为美国邮政服务地址更改服务Keyline,但似乎我得到了错误的数字!我们的输入字符串中只有数字,使计算更容易一些。当我将结果与测试应用程序的结果进行比较时,我得到的数字不同。我不明白发生了什么事?有没有人看到我的算法有什么问题?它一定是明显的东西...USPS ACS Keyline Check Digit
该方法的文档,可以在这个文件的12-13页上找到: http://ribbs.usps.gov/acs/documents/tech_guides/KEYLINE.EXE
: http://www.usps.com/cpim/ftp/pubs/pub8a.pdf
示例应用程序可以在这里找到请注意:我根据论坛用户的帮助修复了下面的代码。这是为了让未来的读者能够完整地使用这些代码。
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 = ''
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
set @check_digit = convert(varchar, (10 - (@total_value % 10)) % 10)
为什么我们有一个额外的国防部:
转换(VARCHAR,10% < < -
文件称,只有最后一个数字需要从扣除? 10.我错过了什么吗?
额外的mod 10是我最后只有最右边的数字。 – Jasmine 2009-02-19 19:45:14
我不确定你为什么搞乱当你使用基于集合的语言工作时,整个字符串表示。
我可能会像下面这样做。我跑了四次测试,他们都成功了。如果你真的想这么做,你可以很容易地扩展这个角色来处理角色,甚至可以让表格永久化。
CREATE FUNCTION dbo.Get_Mod10
(
@original_string VARCHAR(26)
)
RETURNS VARCHAR(30)
AS
BEGIN
DECLARE
@value_mapping TABLE (original_char CHAR(1) NOT NULL, odd_value TINYINT NOT NULL, even_value TINYINT NOT NULL)
INSERT INTO @value_mapping
(
original_char,
odd_value,
even_value
)
SELECT '0', 0, 0 UNION
SELECT '1', 2, 1 UNION
SELECT '2', 4, 2 UNION
SELECT '3', 6, 3 UNION
SELECT '4', 8, 4 UNION
SELECT '5', 1, 5 UNION
SELECT '6', 3, 6 UNION
SELECT '7', 5, 7 UNION
SELECT '8', 7, 8 UNION
SELECT '9', 9, 9
DECLARE
@i INT,
@clean_string VARCHAR(26),
@len_string TINYINT,
@sum SMALLINT
SET @clean_string = REPLACE(@original_string, ' ', '')
SET @len_string = LEN(@clean_string)
SET @i = 1
SET @sum = 0
WHILE (@i <= @len_string)
BEGIN
SELECT
@sum = @sum + CASE WHEN @i % 2 = 0 THEN even_value ELSE odd_value END
FROM
@value_mapping
WHERE
original_char = SUBSTRING(@clean_string, @i, 1)
SET @i = @i + 1
END
RETURN (10 - (@sum % 10)) % 10
END
GO
那么,我做了一个明确的实现,因为我希望将来它对程序员是可以理解的,而且这个商店里没有人会说SQL。另外,当我做了类似的事情时,它不起作用。将在今天晚些时候尝试你的想法,并告诉你它是否有效。谢谢! – Jasmine 2009-02-19 19:46:40
DUH!我知道我有东西倒退:) 谢谢! – Jasmine 2009-02-19 20:02:10