














Copyright © 2004
by Polaris Computing.
All rights reserved.
| |
| |
SQL Code for ISBN
Conversion
Following is a SQL function that converts ISBN-10
to ISBN-13. You can add it to your SQL system and use it with any
table on that machine. This way you can easily convert your existing
ISBN's to the new.
If you use IBM AS/400 DB2, you can copy the
following code into a source file and run the RUNSQLSTM command to create
the function. Then this function will be available for your whole SQL
system. Modifications might be needed if you use other SQL systems.
|
Language |
SQL |
|
Compiler |
IBM AS/400 DB2 |
|
Disclaimer: All sample
code contained herein is provide to you "AS IS." ALL IMPLIED
WARRANTIES, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF THE
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY
DISCLAIMED. |
Create Function ISBNCNV(ISBN0 Char(10)) Returns Char(13)
Language SQL
Begin
Declare NewISBN Char(12);
Declare i integer;
Declare n integer;
Declare v integer;
Set NewISBN = '978' || SubStr(ISBN0, 1, 9);
Set n = 0;
Set i = 1;
LoopSum:
Loop
If i>12 Then
Leave LoopSum;
Else
Set v = Int(SubStr(NewISBN, i, 1));
If Mod(i, 2) = 0 Then
Set n = n + 3 * v;
Else
Set n = n + v;
End If;
Set i = i + 1;
End If;
End Loop;
Set n = Mod(n, 10);
If n<>0 Then
Set n = 10 - n;
End If;
Return NewISBN || Char(n);
End
|
|
|