You could try this, it produces the right output for me...
create function [dbo].[Code128Barcode](@input varchar(3997), @table char(1) = 'B')
returns table
with schemabinding
as
--=======================================================================--
--== Function to build the required data string for a code 128 barcode ==--
--== Code written from a description of the specification by ==--
--== Mister Magoo - find me on SSC or twitter @mistermag00 ==--
--== NOTE: this code does not validate that you are passing in valid ==--
--== data for code 128 barcodes. SISO ==--
--=======================================================================--
return (
-- build an inline fast tally table
with n10(n) as (
select 1 union all select 1 union all
select 1 union all select 1 union all
select 1 union all select 1 union all
select 1 union all select 1 union all
select 1 union all select 1
),
n100(n) as (
select 1 from n10 a, n10 b
),
n10000(n) as (
select 1 from n100 a, n100 b
),
tally(n) as (
-- limit the tally table to the exact length of the input string
select top(len(@input)) row_number() over(order by @@spid) as n from n10000 order by n
)
-- Now the query that does the "work"
select
-- in Code 128, the "table" is denoted by one of three START codes
case @table
when 'A' then char(203)
when 'B' then char(204)
when 'C' then char(205)
end -- START
+ @input -- Original string
-- The code 128 Checksum is made up by taking the sum of each
-- of the input string's characters numbers (ASCII - 32)
-- multiplied by it's position in the string , plus the character
-- number of the START character, then taking MODULUS 103
-- This number is converted to ASCII by adding 32
+ char(case when (sum(n*(ascii(substring(@input,n,1))-32))+case @table when 'A' then 103 when 'B' then 104 when 'C' then 105 end)%103>94 then 100
else 32
end+(sum(n*(ascii(substring(@input,n,1))-32))+case @table when 'A' then 103 when 'B' then 104 when 'C' then 105 end)%103)
-- The STOP character never changes and denotes the end of the barcode.
+ CHAR(206) -- STOP
-- we need an alias for the column
AS encoded_for_128
from tally
)
edit:increased number of rows in the inline tally
edit:fixed incorrect markers
edit:fixed high end checksums - properly this time
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);