Barcode 128 generator (using scalar function)

  • I'm looking for a barcode generating Function that uses the standard Code128. Apparently no function that fixes this has been posted before in the forum.

    The way the barcode functions work is always the same: you pass a string with the source code ('bananas') and you get the coded barcode string ( ÑbananasÈÓ, always starts with capital Ñ and ends with Ó).

    I have the right function in Access VBA (code attached), but I'm not able to translate this into SQL Server.

    Does anyone have the equivalent SQL Server function (or knows how to translate this) ?

    Thanks in advance, a.

  • The way the barcode functions work is always the same: you pass a string with the source code ('bananas') and you get the coded barcode string ( ÑbananasÈÓ, always starts with capital Ñ and ends with Ó).

    I expect one or more of the functions/tools people have suggested will do the business.

    But I wanted to point out that Code 128 is not quite like that. Unlike eg Code 39, it is not a straight character-for-character substitution, so just applying a font with static bracketing values doesn't work. It needs a calculated checksum (which is not shown in the human-readable version nor transmitted to the receiving system, but ensures the quality of the data read by the scanner), which is why you need to apply a function to the data you want to encode. And I do not think the control characters are universal - I have seen different font sets that have made different choices with regard to the extended characters they use to represent the control values. Presumably the ones you specify will work for your environment.

  • It depends on what it does. There are literally hundreds of barcode formats, even inside the 128 format there are are multiples ways of calculating the output of a given entry.

    The VBA code attached to my 1st message works and it's not that difficult to translate to other languages.

  • a_ud (6/26/2014)


    It depends on what it does. There are literally hundreds of barcode formats, even inside the 128 format there are are multiples ways of calculating the output of a given entry.

    The VBA code attached to my 1st message works and it's not that difficult to translate to other languages.

    Don't bother - that post you replied to is spam.

    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]

  • One my web application uses barcodes.... When i add a new item i am generating a barcode value which is converted to a barcode image... My question is what would you suggest storing barcode values or barcode images in sql server 2005?What Type of barcode you would suggest using with an asp.net application?Linear Barcode (Code 128) is my choice.. Any other suggestion....

  • a_ud (6/26/2014)


    The VBA code attached to my 1st message works and it's not that difficult to translate to other languages.

    If that were true, you wouldn't be asking for help, now would you? 😉

    This is actually pretty easy to pull off in SQL Server including the CheckSum character but I need you to answer a question first. According to the start character that you've used, it implies that you will always be using Barcode 128 "B". Is that true or will you make excursions to Barcode 128 "A" and/or Barcode 128 "C"?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Ewan Hampson (4/8/2013)


    The way the barcode functions work is always the same: you pass a string with the source code ('bananas') and you get the coded barcode string ( ÑbananasÈÓ, always starts with capital Ñ and ends with Ó).

    I expect one or more of the functions/tools people have suggested will do the business.

    But I wanted to point out that Code 128 is not quite like that. Unlike eg Code 39, it is not a straight character-for-character substitution, so just applying a font with static bracketing values doesn't work. It needs a calculated checksum (which is not shown in the human-readable version nor transmitted to the receiving system, but ensures the quality of the data read by the scanner), which is why you need to apply a function to the data you want to encode. And I do not think the control characters are universal - I have seen different font sets that have made different choices with regard to the extended characters they use to represent the control values. Presumably the ones you specify will work for your environment.

    But, once you've calculated the CheckSum character, it is just as easy as applying the font on a 1:1 basis. Attaining the proper 128-B True Type font isn't all that difficult and neither is calculating the ChecksSum character in T-SQL. I'm working on that for BarCode 128-B right now.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • a_ud (2/17/2011)


    I'm looking for a barcode generating Function that uses the standard Code128. Apparently no function that fixes this has been posted before in the forum.

    The way the barcode functions work is always the same: you pass a string with the source code ('bananas') and you get the coded barcode string ( ÑbananasÈÓ, always starts with capital Ñ and ends with Ó).

    I have the right function in Access VBA (code attached), but I'm not able to translate this into SQL Server.

    Does anyone have the equivalent SQL Server function (or knows how to translate this) ?

    Thanks in advance, a.

    Here you go. Details are in the code as they should be. Please do read the comments because this function won't handle excursions to Type A or C, yet. Example usage included in the comments.

    CREATE FUNCTION dbo.BarCode128

    /**********************************************************************************************************************

    Pupose:

    Given a string of legal "Type B" Barcode 128 characters, return the original string with StartCode, Checksum, and

    StopCode characters.

    Note that this will currently only handle "Type B" and no excursions to "Type A" or "Type C" are allowed. The

    @pBarCodeStart is for future exapansion to handle such things.

    Usage:

    SELECT BarCode128

    FROM dbo.BarCode128('B','bananas') --ÑbananasÈÓ expected

    ;

    References:

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

    http://courses.cs.washington.edu/courses/cse370/01au/minirproject/BarcodeBattlers/barcodes.html

    Revision History:

    Rev 00 - 05 Feb 2015 - Jeff Moden

    - Intial creation. Handles only legal "Type B" characters and no excursions to "Type A" or "Type C".

    **********************************************************************************************************************/

    (

    @pBarCodeStart CHAR(1)

    ,@pString VARCHAR(8000)

    )

    RETURNS TABLE AS --iSF or "Inline Scalar Function", in this case.

    RETURN WITH

    cteCheckSum AS

    (

    SELECT CheckSumValue = (

    ASCII(@pBarCodeStart)+38 --StartCode Value (38 is 103-65 and 65 is "A")

    +SUM((ASCII(SUBSTRING(@pString,t.N,1))-32)*t.N)

    )%103+32

    FROM dbo.fnTally(1,LEN(@pString)) t

    )

    SELECT BarCode128 =

    CHAR(ASCII(@pBarCodeStart)+143) --StartCode Character (143 is 200-65 and 65 is "A")

    + @pString --Original String

    + CHAR(CheckSumValue + CASE WHEN CheckSumValue < 127 THEN 0 ELSE 73 END) --CheckSum Character

    + CHAR(211) --StopCode Character

    FROM cteCheckSum

    WHERE @pBarCodeStart = 'B'

    ;

    Here's the code for the dbo.fnTally function. If you don't know what it is or how it works, see the "references" in the comments in the code.

    CREATE FUNCTION [dbo].[fnTally]

    /**********************************************************************************************************************

    Purpose:

    Return a column of BIGINTs from @ZeroOrOne up to and including @MaxN with a max value of 1 Billion.

    As a performance note, it takes about 00:02:10 (hh:mm:ss) to generate 1 Billion numbers to a throw-away variable.

    Usage:

    --===== Syntax example (Returns BIGINT)

    SELECT t.N

    FROM dbo.fnTally(@ZeroOrOne,@MaxN) t

    ;

    Notes:

    1. Based on Itzik Ben-Gan's cascading CTE (cCTE) method for creating a "readless" Tally Table source of BIGINTs.

    Refer to the following URLs for how it works and introduction for how it replaces certain loops.

    http://www.sqlservercentral.com/articles/T-SQL/62867/

    http://sqlmag.com/sql-server/virtual-auxiliary-table-numbers

    2. To start a sequence at 0, @ZeroOrOne must be 0 or NULL. Any other value that's convertable to the BIT data-type

    will cause the sequence to start at 1.

    3. If @ZeroOrOne = 1 and @MaxN = 0, no rows will be returned.

    5. If @MaxN is negative or NULL, a "TOP" error will be returned.

    6. @MaxN must be a positive number from >= the value of @ZeroOrOne up to and including 1 Billion. If a larger

    number is used, the function will silently truncate after 1 Billion. If you actually need a sequence with

    that many values, you should consider using a different tool. ;-)

    7. There will be a substantial reduction in performance if "N" is sorted in descending order. If a descending

    sort is required, use code similar to the following. Performance will decrease by about 27% but it's still

    very fast especially compared with just doing a simple descending sort on "N", which is about 20 times slower.

    If @ZeroOrOne is a 0, in this case, remove the "+1" from the code.

    DECLARE @MaxN BIGINT;

    SELECT @MaxN = 1000;

    SELECT DescendingN = @MaxN-N+1

    FROM dbo.fnTally(1,@MaxN);

    8. There is no performance penalty for sorting "N" in ascending order because the output is explicity sorted by

    ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    Revision History:

    Rev 00 - Unknown - Jeff Moden

    - Initial creation with error handling for @MaxN.

    Rev 01 - 09 Feb 2013 - Jeff Moden

    - Modified to start at 0 or 1.

    Rev 02 - 16 May 2013 - Jeff Moden

    - Removed error handling for @MaxN because of exceptional cases.

    **********************************************************************************************************************/

    (@ZeroOrOne BIT, @MaxN INT)

    RETURNS TABLE WITH SCHEMABINDING AS

    RETURN WITH

    E1(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) --10E1 or 10 rows

    , E3(N) AS (SELECT 1 FROM E1 a, E1 b, E1 c) --10E3 or 1 Thousand rows

    , E9(N) AS (SELECT 1 FROM E3 a, E3 b, E3 c) --10E9 or 1 Billion rows

    SELECT N = 0 WHERE ISNULL(@ZeroOrOne,0)= 0 --Conditionally start at 0.

    UNION ALL

    SELECT TOP(@MaxN) N = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E9 -- Values from 1 to @MaxN

    ;

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks Jeff!

    I wasn't expecting an answer now, but it's still useful. I'll definitely test it in my system.

  • Hi Jeff,

    I have one also that handles A, B and C

    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]

  • mister.magoo (2/5/2015)


    Hi Jeff,

    I have one also that handles A, B and C

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

    Absolutely awesome! I could be wrong but it doesn't look like it would handle the "SHIFT", "CODE", and nested "START CODE" eventualities. Regardless that would make for a great article for "flat" conversions.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (2/5/2015)


    mister.magoo (2/5/2015)


    Hi Jeff,

    I have one also that handles A, B and C

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

    Absolutely awesome! I could be wrong but it doesn't look like it would handle the "SHIFT", "CODE", and nested "START CODE" eventualities. Regardless that would make for a great article for "flat" conversions.

    Absolutely right, it doesn't deal with nested start codes, as I have never had need of them, but I would be interested to see your final version.

    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]

  • It seems doesn't have. Anyway, I did not find it ...

    ---------------------

    Chargeur Sony Xperia Z4

  • Viewing 13 posts - 1 through 12 (of 12 total)

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