Barcode font 128b -SQL function

  • Hi,

    I am looking for an SQL function that will generate the correct characters to make barcode font code128 work when used in Microsoft Word. The data is passed to word with the data tag using the barcode font.

    This is a web link to the rules and where the font can be downloaded.

    http://grandzebu.net/informatique/codbar-en/code128.htm

    A bit more info on the font here

    http://en.wikipedia.org/wiki/Code_128

    And I tried

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=70901

    http://www.sqlservercentral.com/Forums/Topic1065554-149-1.aspx

    http://www.sqlservercentral.com/Forums/Topic1062489-1291-1.aspx

    But none of these solutions appear to work when tested with a barcode reader. The bar code reader does work because we tested it against web sites generating the barcodes and it is code128b we are working with

    Anyone any ideas/SQL functions that do work.

    Many thanks

    Eliza

  • 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);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Thanks mister.magoo, this works for me!

  • frisko (4/11/2014)


    Thanks mister.magoo, this works for me!

    You are most welcome - glad it is of use.

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • thanks M.M.

    I have used this code as my sql function, and returning barcode value using this function to SSRS.

    And in rdlc file I am using Code128 as font; but I am getting boxes at the start and at the end of the barcode.

    So, the barcode is not readable from Barcode scanner.

    Please help!!! :crying:

  • patilyatin542 (9/29/2014)


    thanks M.M.

    I have used this code as my sql function, and returning barcode value using this function to SSRS.

    And in rdlc file I am using Code128 as font; but I am getting boxes at the start and at the end of the barcode.

    So, the barcode is not readable from Barcode scanner.

    Please help!!! :crying:

    There was an error in that code - don't know how it got past QC to be honest!

    I have modified the code - should work now 😀

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • thanks M.M for reverting back!!!

    please upload the updated code, you are saying. 🙁

  • patilyatin542 (10/2/2014)


    thanks M.M for reverting back!!!

    please upload the updated code, you are saying. 🙁

    I already changed the code.

    http://www.sqlservercentral.com/Forums/FindPost1470956.aspx

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • hi, M.M.

    :: Code 128 ::

    I have used your code in sql which you described in http://www.sqlservercentral.com/Forums/FindPost1470956.aspx

    and returning value of a column like SELECT encoded_for_128 FROM dbo.Code128Barcode('140000009','B') through procedure to winform, and there I am using dataset to fill values using this procedure; and then filling table adapter and all these things. In SSRS, I have set barcode font to "Code 128" & I have set barcode font size to 30pt, well on execution of program I get barcode printed properly; but scanner doesn't scans it. Am I comiting any kind of mistake???

    :: Bar-Code 39 ::

    I also have used this bar-code in my program, in this case I don't have to use sql function; just I have to add "*" at the start and at the end of my value in SSRS, as it indicates barcode value is between those "*". On execution, it prints properly on font size of 20 pt, but doesn't scans. And on using wordpad, I am setting same value, same font size, same font; it prints and scans well.

    Any Idea?? behind these.

    One thing I have noticed is; when using my program to print bar-code, thickness of printed barcode is greater than barcode thickness printed using wordpad., I am attaching images as below:-

    Using Wordpad=>

    http://postimage.org/image/cvdqpchff/

    Using my WinForm project=>

    http://postimage.org/image/tlp4eofuj/

    Plz help!!!

    :unsure:

  • patilyatin542 (10/10/2014)


    hi, M.M.

    :: Code 128 ::

    I have used your code in sql which you described in http://www.sqlservercentral.com/Forums/FindPost1470956.aspx

    and returning value of a column like SELECT encoded_for_128 FROM dbo.Code128Barcode('140000009','B') through procedure to winform, and there I am using dataset to fill values using this procedure; and then filling table adapter and all these things. In SSRS, I have set barcode font to "Code 128" & I have set barcode font size to 30pt, well on execution of program I get barcode printed properly; but scanner doesn't scans it. Am I comiting any kind of mistake???

    :: Bar-Code 39 ::

    I also have used this bar-code in my program, in this case I don't have to use sql function; just I have to add "*" at the start and at the end of my value in SSRS, as it indicates barcode value is between those "*". On execution, it prints properly on font size of 20 pt, but doesn't scans. And on using wordpad, I am setting same value, same font size, same font; it prints and scans well.

    Any Idea?? behind these.

    One thing I have noticed is; when using my program to print bar-code, thickness of printed barcode is greater than barcode thickness printed using wordpad., I am attaching images as below:-

    Using Wordpad=>

    http://postimage.org/image/cvdqpchff/

    Using my WinForm project=>

    http://postimage.org/image/tlp4eofuj/

    Plz help!!!

    :unsure:

    The barcode printed from your Winforms project seems to be "fuzzy" - I would look at how you are rendering it to the printer - maybe it is being converted to a low resolution bitmap for printing?

    does it look OK on screen? can you scan your Winforms barcode from the screen using a smartphone (they don't mind scanning screens as they don't use lasers)?

    I can certainly scan code 128 barcodes produced in this way in SSRS...

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Implemented this function and it seems to work great. I have one problem; I'm getting some checksum characters that my barcode font doesn't seem to be able to handle.

    Example strings generated:

    ÌCY20012X01-PlywoodVÎ - Barcode works.

    ÌCY21022X01-SpindleƒÎ - Barcode doesn't work.

    ÌCY20088X01-Pwood_off_cut†Î - Barcode doesn't work.

    I'm new to this barcode stuff but I'm guessing this is a problem with the font I'm using.

    Does anyone know I'm correct, and of a font that will work??

    Thanks

  • trwillcox (10/14/2014)


    Implemented this function and it seems to work great. I have one problem; I'm getting some checksum characters that my barcode font doesn't seem to be able to handle.

    Example strings generated:

    ÌCY20012X01-PlywoodVÎ - Barcode works.

    ÌCY21022X01-SpindleƒÎ - Barcode doesn't work.

    ÌCY20088X01-Pwood_off_cut†Î - Barcode doesn't work.

    I'm new to this barcode stuff but I'm guessing this is a problem with the font I'm using.

    Does anyone know I'm correct, and of a font that will work??

    Thanks

    Nope, you found a bug in the function 😎

    Corrected code found here (Back a page) http://www.sqlservercentral.com/Forums/FindPost1470956.aspx

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Hi MM,

    Thanks very much for taking a look and replying to my post.

    The fix seems to have solved the problem for the two examples I posted but introduced the problem for other codes? I've listed some examples below, any chance you could take a look? Any help you could give would be hugely appreciated.

    ÌCY28946X01-Pad_Bolt_5inŽÎ

    ÌCG68796X01-Devon_RailœÎ

    ÌCY01012X01-Posts__MTO®Î

    ÌCY10093X01-Cove_Form£Î

    ÌCY10103X01-Altro_Silicone³Î

    ÌCY21020X01-50mm_Angle_Plas¿Î

    ÌCY21321X01-Door_Closer™Î

    ÌCY21667X01-Mortice_latch¬Î

    ÌCY25682X01-WasherÁÎ

    ÌCY29540X01-Pine_WedgeµÎ

    ÌCY29542X01-Timber±Î

    Thanks

  • I was just testing to see if you were paying attention 😎

    Hopefully all solved now - certainly looks right for all the problems you've shown so far.

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Just tested about 1000 barcodes generated with the updated code and they all worked.

    Thanks for all your help MM. Hopefully I wont be bothering you again!!

  • Viewing 15 posts - 1 through 15 (of 20 total)

    You must be logged in to reply to this topic. Login to reply