Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# db2 ean13 checkdigit calculate by trigger

Posted on 2010-11-16
Medium Priority
1,451 Views
HI,
I 'd like to calculate by trigger the 13th character of an EAN13 partnumber code.
I did the code in VBA, but now I'd like to translate it in order to
insert it in a db2 trigger (v5r4m0).

I have a table1 with field PN12, and field checkdigit,
how to fill checkdigit with right digit calculated using the code below,
how to transform this code in a trigger?
``````Function CheckDigitEAN13(PN12 As String) As Integer

Factor = 3
WeightedTotal = 0
For I = Len(PN12) To 1 Step -1
'Get the value of each number starting at the end
CurrentCharNum = Mid(PN12, I, 1)
'Multiply by the weighting factor which is 3,1,3,1...
WeightedTotal = WeightedTotal + CurrentCharNum * Factor
'Change factor for next calculation
Factor = 4 - Factor
Next I
'Find the CheckDigit by finding the number + WeightedTotal that = a multiple of 10
'Divide by 10, get the remainder and subtract from 10
I = (WeightedTotal Mod 10)
If I <> 0 Then
CheckDigit = (10 - I)
Else
CheckDigit = 0
End If

CheckDigitEAN13 = CheckDigit

End Function
``````
0
Question by:bobdylan75

LVL 37

Assisted Solution

momi_sabag earned 400 total points
ID: 34146877
the best solution would be to create your own function that does that
take a look here
http://publib.boulder.ibm.com/infocenter/iseries/v5r4/topic/db2/rbafzmstcfsc.htm#cfsc
and
http://publib.boulder.ibm.com/infocenter/iseries/v5r4/topic/db2/rbafzmstsqlcontstmts.htm#sqlcontstmts

to get an idea how this can be done

then, in the trigger you just call your function like any other function
0

LVL 46

Expert Comment

ID: 34146887
Hi Bob,

If you're familiar with SQL coding, it's pretty easy.  :)  The basics of a Trigger are wrapped around the calculation.  Because of the coding limitations of triggers, it might be easiest to write the CheckDigit calculation as a UDF, then call the function within the trigger.

What do you want to do with the check-digit?  Append it to a string or store it into its own column?

Kent
0

Author Comment

ID: 34147068
Hi Kdo,
I want to append it to the PartNumber 12 chars so I have my EAN13 code
and I can print label with barcode..
I've only a little experience with trigger and I never use UDF before, can you explein my better?
0

LVL 27

Expert Comment

ID: 34152115
...in order to insert it in a db2 trigger

A "trigger" doesn't seem appropriate. By putting the code into a trigger, the code will execute every time the trigger event (INSERT or UPDATE) happens -- even if the update program doesn't need it to run.

It would be reasonable to have a trigger verify that a check-digit was correct, and possibly even to replace invalid check-digits with correct ones (though I'd question the wisdom of such a trigger). But it doesn't seem quite right in this case. It could fire even if someone was updating columns that didn't require the credit card number itself.

And I'm not totally clear if a UDF makes sense, though it seems possibly more sensible than a trigger.

I can give you a UDF that accepts a 12-char number and returns a 13-char version, or returns just the check digit as a character or an integer. But I'm wondering where you'd put any of it to use.

What is the work-flow? Are you somehow receiving a file with just the 12-char numbers and you need to convert them? Can you describe the process? Maybe the best solution is at some other step.

Tom
0

Author Comment

ID: 34153492
The user insert an new internal part number for a new product they sell.
So I transform this new part number in a 12 chars part number for ean13.
So I have to create the ean13 code, just adding the 13th character.
Now I made it on Windows PC.
I want to use online db2.
I understand that I have to learn UDF and so to use this UDF function
on a simple trigger Insert/update.
It's this right?
0

Author Comment

ID: 34153496
(the 13th character is the checkdigit character calculated with the vb code before.)
0

LVL 16

Expert Comment

ID: 34172771
Do you need this calculation in SQL or in a RPG-Trigger program,
I have both of them for you.... the SQL is complex to read, the RPG I have to dig in our system :-)

0

Author Comment

ID: 34172912
I'd like, if is possible, to have first RPG one ;) thanks..
0

LVL 16

Expert Comment

ID: 34187532
Here a working example of the EAN controll digit Inputparm = 13 pos Character,
the return value is the complete barcode.
`````` *-------------------------------------------------------------------------
* Fields definitions
*-------------------------------------------------------------------------
D EAN13           DS            13
D EAN                            1A   DIM(13)
D CALC            S              5S 0
D CTRL            S              1S 0
*-------------------------------------------------------------------------
* Main line
*-------------------------------------------------------------------------
C                   EVAL      CALC =
C                                 %DEC(EAN(02):1:0) + %DEC(EAN(04):1:0) +
C                                 %DEC(EAN(06):1:0) + %DEC(EAN(08):1:0) +
C                                 %DEC(EAN(10):1:0) + %DEC(EAN(12):1:0)

C                   EVAL      Calc = Calc * 3

C                   EVAL      CALC = CALC +
C                                 %DEC(EAN(01):1:0) + %DEC(EAN(03):1:0) +
C                                 %DEC(EAN(05):1:0) + %DEC(EAN(07):1:0) +
C                                 %DEC(EAN(09):1:0) + %DEC(EAN(11):1:0)

C                   EVAL      Ctrl    = 10 - %Rem(Calc:10)
C                   EVAL      EAN(13) = %editc(Ctrl:'X')

C                   EVAL      *InLr = *On
*-------------------------------------------------------------------------
* Klist + Plist definitions
*-------------------------------------------------------------------------
C     *ENTRY        PLIST
C                   PARM                    EAN13
``````

Good Luck
Murph
0

Author Comment

ID: 34187565
Sorry but thanks for the rpgle program,
but I don't know how to implement the trigger to link the rpgle program,
because this is a simple rpgle program at the moment.
0

LVL 16

Expert Comment

ID: 34187850
That is what you asked for

"I'd like, if is possible, to have first RPG one ;) thanks.."
0

LVL 16

Expert Comment

ID: 34187911
You can call this from a trigger program, but beware there is NO check on the contents of the input parm,
so non nuneric data gives errors, how sure are you that the data in the table is numeric or filled with numeric fields only.

I understand you like one of us to write the trigger program for you?

0

Author Comment

ID: 34187954
Exactly.. ;-)
My question was how to write a trigger ...
I Don't now the syntax to call the rpgle program and all the rest..
My first question was to use only sql but seen that I currently use rpg
I think that is a good idea use rpg but now
remains to understand how to call the rpg program from the trigger.
0

Author Comment

ID: 34187965
the data control is a my problem ;))
0

LVL 16

Expert Comment

ID: 34188132
Give me the file name you like to trigger and the field name of the EANcode.

0

LVL 16

Expert Comment

ID: 34188194
Oh and tell me what must be done if the field is not filled with 12 or 13 digits or the digits are not numeric

0

Author Comment

ID: 34188240
Thank you very very very much Murphey!
to semplify:
table name: EanTable.
Field 12 chars name: PartNumber12
Field 13 chars name: PartNumber13

If PartNumber12 is empty or has not 12 chars or is not a number, it could write in PartNumber13 a value like "ERROR".

Thank you very much!
0

LVL 16

Accepted Solution

theo kouwenhoven earned 1600 total points
ID: 34188678
Hi bobdylan75,

Attached you find 2 sources 1 is checking the barcode  the other is the trigger.

You have to add the trigger twice one time for Insert and one time for update
both *before  and with parameter ALWREPCHG(*YES)

Be carefull, becaus I don't have your file here, I was not able to test, so applie
it first in a test environemt and change the fields withe.g. DFU for full controll

Good luck
BAR13CD.txt
TRG001R.txt
0

LVL 16

Expert Comment

ID: 34204149
Hey Hi bobdylan75,

How are you dooing?

Is it up and running?

0

Author Comment

ID: 34205829
;)
sorry I was away..
wow but TRG001R is a trigger made with rpg?
I have to compile it like a simple rpgle program?
Tomorrow I will test it. Today I cannot sorry.
0

LVL 16

Expert Comment

ID: 34206369
Yes just compile is like all other RPG...
(The other program too) :)

0

Author Closing Comment

ID: 34212515
thank you so much! in the next days I will be here for a consult again
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm nâ€¦
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people unaâ€¦
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, youâ€¦
###### Suggested Courses
Course of the Month13 days, 15 hours left to enroll