문제
를 구현하고 있습니다"MOD10"체크 숫자 알고리즘을 사용하여 SQL 을 위해 우리가 우편 서비스에 주소를 변화 서비스 도련을 방법에 따라서 그들의 문서지만,그것은 보인다고 잘못된 숫자!우리의 입력 문자열에만 번호를 만들고,그들을 계산 좀 더 쉽게 할 수 있습니다.을 비교할 때 내가 결과와 결과를 자신의 테스트는 응용 프로그램,내가 다른 번호를 사용합니다.난 이해하지 못하에게 무슨 일이?사람이 아무것도 보이 잘못 알고리즘을 내?그것은 무언가를 명백하...
문서에 대한 방법은 페이지에서 찾을 수 있습니다 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
추가 모드가있는 이유 :
변환 (Varchar, 10 % <<-- ?
이 문서에 따르면 마지막 숫자 만 10에서 빼면 필요하다고 말합니다.
제휴하지 않습니다 StackOverflow