# db2 ean13 checkdigit calculate by trigger

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...
'and add the sum together
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
``````
###### Who is Participating?

Application ConsultantCommented:
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

Commented:
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

Data Warehouse Architect / DBACommented:
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 Commented:
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

Commented:
...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 Commented:
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 Commented:
(the 13th character is the checkdigit character calculated with the vb code before.)
0

Application ConsultantCommented:
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 Commented:
I'd like, if is possible, to have first RPG one ;) thanks..
0

Application ConsultantCommented:
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 Commented:
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

Application ConsultantCommented:
That is what you asked for

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

Application ConsultantCommented:
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 Commented:
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 Commented:
the data control is a my problem ;))
0

Application ConsultantCommented:
Give me the file name you like to trigger and the field name of the EANcode.

0

Application ConsultantCommented:
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 Commented:
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

Application ConsultantCommented:
Hey Hi bobdylan75,

How are you dooing?

Is it up and running?

0

Author Commented:
;)
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

Application ConsultantCommented:
Yes just compile is like all other RPG...
(The other program too) :)

0

Author Commented:
thank you so much! in the next days I will be here for a consult again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.