SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Barcode 128 generator (using scalar function)


Barcode 128 generator (using scalar function)

Author
Message
a_ud
a_ud
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 336
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.
Attachments
basCode128.txt (147 views, 3.00 KB)
Ewan Hampson
Ewan Hampson
Old Hand
Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)Old Hand (300 reputation)

Group: General Forum Members
Points: 300 Visits: 1826
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.
a_ud
a_ud
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 336
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.
mister.magoo
mister.magoo
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4294 Visits: 7865
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
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • andysecondday2
    andysecondday2
    Forum Newbie
    Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

    Group: General Forum Members
    Points: 1 Visits: 2
    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....
    Jeff Moden
    Jeff Moden
    SSC Guru
    SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

    Group: General Forum Members
    Points: 90303 Visits: 41146
    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.
    If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

    Helpful Links:
    How to post code problems
    How to post performance problems
    Forum FAQs
    Jeff Moden
    Jeff Moden
    SSC Guru
    SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

    Group: General Forum Members
    Points: 90303 Visits: 41146
    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.
    If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

    Helpful Links:
    How to post code problems
    How to post performance problems
    Forum FAQs
    Jeff Moden
    Jeff Moden
    SSC Guru
    SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

    Group: General Forum Members
    Points: 90303 Visits: 41146
    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.
    If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

    Helpful Links:
    How to post code problems
    How to post performance problems
    Forum FAQs
    a_ud
    a_ud
    SSC-Enthusiastic
    SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

    Group: General Forum Members
    Points: 168 Visits: 336
    Thanks Jeff!
    I wasn't expecting an answer now, but it's still useful. I'll definitely test it in my system.
    mister.magoo
    mister.magoo
    SSCarpal Tunnel
    SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

    Group: General Forum Members
    Points: 4294 Visits: 7865
    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
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search