题
我已经实现了“MOD 10”使用SQL校验位算法,为美国邮政服务的地址更改服务KEYLINE根据自己的文档中的方法,但似乎我得到了错误的号码!我们输入的字符串只有一些数字,使计算变得更容易一些。当我比较我的结果与他们的测试应用程序的结果,我得到不同的数字。我不明白这是怎么回事?有谁看到什么毛病我的算法?它一定是明显的东西...
的方法的文档可以在这个文件的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
解决方案
set @check_digit = convert(varchar, (10 - (@total_value % 10)) % 10)
其他提示
我不知道为什么你会用,当你在一个基于集合的语言正在使用的整个字符串表示搞乱。
我可能会不喜欢它的下面。我通过跑了四次测试,他们均获得成功。您可以轻松地扩展这个处理字符,以及和你甚至可以使表永久如果你真的想这样做。
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
我们
为什么有一个额外的MOD:
转换(VARCHAR,的 10%强> << - ?
该文件说,只有最后一个数字需要被减去10,我错过了什么?
不隶属于 StackOverflow