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