How do I get the last digit (15th luhn check digit) for a given 14 digit number with sql server -
i have 14 digit number, 86162200359545 have append luhn digit make complete. how digit sql server. thanks.
to append single digit multiply ten , add
declare @n bigint = 86162200359545 declare @d int = 0 select @n * 10 + @d
to calculate check digit, use numbers table split string digits, , apply calculation
declare @data table (id varchar(15)) insert @data (id) values ('7992739871') select luhn_id = cast(id bigint) * 10 + d ( select id, d = sum(cast(case when f > 9 (f / 10) + ((f * 1.0 / 10 - 1) * 10) else f end int)) * 9 % 10 ( select id, f = cast(substring(id, n, 1) int) * case when n % 2 = 1 1 else 2 end @data cross apply ( select top (len(id)) n = row_number() on (order (select 1)) (select 1 union select 1 union select 1) x cross join (select 1 b union select 1 union select 1) y cross join (select 1 c union select 1 union select 1) z ) numbers ) t group id ) t
Comments
Post a Comment