Talk:Algorithm Implementation/Checksums/Damm Algorithm

CREATE FUNCTION [dbo].[fnDammChecksum] ( @invar VARCHAR(32) )

RETURNS VARCHAR(1)

AS

BEGIN

-- Implementation of DAMM checksum algorithm from wikipedia

--https://en.wikipedia.org/wiki/Damm_algorithm

DECLARE @Damm CHAR(100)

= '0317598642' + '7092154863' + '4206871359' + '1750983426' + '6123045987'

+ '3674209581' + '5869720134' + '8945362017' + '9438617205' + '2581436790'

DECLARE @interim TINYINT = 0, @len TINYINT, @pos TINYINT = 1

SET @len=LEN(@invar)

WHILE (@pos <= @len)

BEGIN

SET @interim = SUBSTRING(@damm, @interim * 10 + SUBSTRING(@invar, @pos, 1) + 1, 1 )

SET @pos=@pos+1

END RETURN CONVERT(VARCHAR(1), @interim)

END

66.49.252.70 (discuss) 02:33, 11 November 2015 (UTC)