Vous êtes sur la page 1sur 5

Converting a numeric currency data to a text in DB2 using SQL

By PrasannaLakshmi Kona

Executive Summary

DB2 Universal Database offers several built-in functions like AVG, COUNT, MIN, MAX
and SUM. However, there is no direct way to format currency amount in DB2 UDB
version. I have come up with an approach to effectively exploit the built-in functions
in DB2 to achieve specific currency format within a simple SQL.

Introduction

The objective of this document is to demonstrate a viable approach in DB2 to solve


an inherent problem in the current DB2® Universal Database version related to
currency data format using plain SQL. The idea is to employ a set of available
functions in conjunction with one another to achieve the required formatting.

Conventions in this document: All functions are italicized and capitalized (upper
case) to distinguish them from the actual text. SQL statements and examples are
provided wherever appropriate.

Problem Scenario

There is a reporting requirement to display the Sales Amount in US dollars. The


report needs to be distributed through smart phones (like Blackberry) and PDAs. The
data is available in DB2 and the report needs to be created through a UNIX shell
script with embedded SQL statements (due to cost constraints, no specific reporting
tools were used).

The report is basically a sales summary and would like the following:
East Area
MTD Sales: $546,789,098.44.22
Pending for tomorrow: $123,000.23

As you see, the implicit requirements to display an amount are


a) Prefix the amount with a ‘$’ (dollar) sign,
b) Avoid leading zeroes and
c) Separate the thousands with a ‘,’ (comma)
d) Limit the fractional (decimal/cents) values up to two places

For clarity and at the same time, to keep it simple, let us assume there is a DB2
table that contains the above data as shown below:

<varchar 5> <decimal 15,3> <decimal 15,3>


Area_Name Sales_Amt Pending_Amt
East 546789098.440 123000.239
West 9565732.480 577658.289

Please note that the SQL statements contain the numeric data directly and not the
above table structure so that the code can readily be copied and executed.

1
Assumptions and Constraints

1) DB2 UDB is the database used


2) No standard reporting tools like BO, Cognos are available at the client site.
Hence the option of SQL wrapped in a shell script has been applied. The idea
is to generate a text file that can then be sent to smart phones

Solution Approach

Converting a dollar amount to a currency format in Oracle is fairly simpler through a


single function – TO_CHAR. But in DB2 this is not the case. Below is a series of steps
that needs to be followed to ensure we get the right format. The SQLs are expanded
incrementally in each step, so the final step (Step 8) contains the complete SQL
required to achieve this formatting.

 Step 1 – Trim the decimal part: Trim the decimal part using the DECIMAL
function:-
SELECT DECIMAL (546789098.44, 12, 0) FROM SYSIBM.SYSDUMMY1; (where
12 is the number of digits before the decimals). In DB2, converting a
Number(p,s) data to a character would pad the output with leading zeroes to
fill the data length, so this needs to be trimmed. But such a trimming would
accidentally remove zeroes in the decimal part as well. So, the decimal part is
taken out in this step. This will become clearer in Step 5.
Further this step will help us determine the correct positions for the digits.

 Step 2 - Convert to a text: This would allow us to format the data; e.g.
separate the numbers with a ‘,’ (comma):-
SELECT CHAR (DECIMAL (546789098.44, 12, 0)) FROM SYSIBM.SYSDUMMY1;
Result: 000546789098.
Note that this step will bring the ‘.’ (dot) as well as the leading zeroes to fill
the specified length (12 in this case, since it is a 15,3 decimal number) which
will be removed in the next step.

 Step 3: Remove the trailing dot: Using REPLACE function available in DB2:
SELECT REPLACE (CHAR (DECIMAL (546789098.44, 12, 0)),'.','') FROM
SYSIBM.SYSDUMMY1;
Result: 000546789098

 Step 4 – Formatting the text: Based on the required currency format, add
1000 separators (comma) to format the text using the SUBSTR(). This can be
changed to suit the target currency format.
SELECT SUBSTR (REPLACE (CHAR (DECIMAL (546789098.44, 12, 0)),'.',''), 1,
3) ||','||SUBSTR (REPLACE (CHAR (DECIMAL (546789098.44, 12, 0)),'.',''), 4,
3) ||','|| SUBSTR (REPLACE (CHAR (DECIMAL (546789098.44, 12, 0)),'.',''),
7, 3) ','|| SUBSTR (REPLACE (CHAR (DECIMAL (546789098.44, 12, 0)),'.',''),
10, 3) FROM SYSIBM.SYSDUMMY1;
Result: 000,546,789,098

 Step 5 – Trim leading zeroes: Use Replace () and Trim () to achieve this
For example, if the number was 546789098.01 then the Replace() would take
out the zero in the decima1 part as well. Hence the decimal part is taken out
in Step 1.

2
SELECT
RTRIM(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(
REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRI
M(
REPLACE((SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),1,3)
||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),4,3)||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),7,3)||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),10,3)),'0',' ')),'
','0'),',',' ')),' ',','),'0',' ')),
' ','0'),',',' ')),' ',','),'0',' ')),' ','0'),',',' ')),' ',',')) FROM SYSIBM.SYSDUMMY1;
Result: 546,789,098

 Step 6 – Add the currency symbol: The currency symbol now needs to be
added as required. For example US currency in dollars [‘$’]:-
SELECT RTRIM('$'||
RTRIM(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(
REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRI
M(
REPLACE((SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),1,3)
||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),4,3)||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),7,3)||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),10,3)),'0',' ')),'
','0'),',',' ')),' ',','),'0',' ')),
' ','0'),',',' ')),' ',','),'0',' ')),' ','0'),',',' ')),' ',','))) FROM SYSIBM.SYSDUMMY1;
Result: $546,789,098

 Step 7 – Determine the decimal part: Now that the number has been
converted to a currency format, it is time to determine and add the decimal
part. In our example 44 is the decimal part of the number 546789098.44 that
needs to be added. But before adding this to the text, we first need to do
three things a) locate the decimal part in the number b) convert the decimal
part to a character and c)restrict to the required decimal places in this case it
is 2.
Use the combination of Locate() and Char() functions to achieve this.
SELECT SUBSTR (CHAR (DECIMAL (546789098.44, 15,2)),LOCATE ('.', CHAR
(DECIMAL(546789098.44, 15,2))),LENGTH (CHAR(DECIMAL(546789098.44,
15,2))) - LOCATE ('.', CHAR (DECIMAL(546789098.44, 15,2)))) FROM
SYSIBM.SYSDUMMY1;
Result: .44

 Step 8 – Merge the text from Steps 5 and 6 above: The final query would look
like below-
SELECT RTRIM('$'||
RTRIM(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(
REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRIM(REPLACE(REPLACE(LTRI
M(
REPLACE((SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),1,3)
||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),4,3)||','||
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),7,3)||','||

3
SUBSTR(REPLACE(CHAR(DECIMAL(546789098.44,12,0)),'.',''),10,3)),'0',' ')),'
','0'),',',' ')),' ',','),'0',' ')),
' ','0'),',',' ')),' ',','),'0',' ')),' ','0'),',',' ')),' ',','))) ||SUBSTR (CHAR (DECIMAL
(546789098.44, 15,2)),LOCATE ('.', CHAR (DECIMAL(546789098.44,
15,2))),LENGTH (CHAR(DECIMAL(546789098.44, 15,2))) - LOCATE ('.', CHAR
(DECIMAL(546789098.44, 15,2)))) FROM SYSIBM.SYSDUMMY1;
Final Result: $546,789,098.44

Thus, converting a currency number to a formatted text is a multi-part function as


illustrated. Below is a quick recap of the DB2 functions that are used in the SQL
followed by a comparative analysis of these functions with that available in Oracle
database.

List of DB2 Functions*:

S.No Function Name Function description


1 CHAR Number to Character Conversion
2 REPLACE Replace a string or a word
3 LEFT Replace a Left most String
Remove leading and trailing characters from a
4 TRIM string
5 INTEGER or INT Number or a Character conversion to an integer
6 RTRIM Remove trailing characters from a string
7 LTRIM Remove leading characters from a string
8 || Concatenate two strings
Returns the starting position of the first
9 LOCATE occurrence of one string within another string
Returns a decimal representation of either a
10 DECIMAL number or a string representation of a number
11 SUBSTR Returns a substring of a string
12 LENGTH Returns length of number or a string
(* functions are restricted to the ones related to the requirement)

Comparison of ORACLE Vs DB2 functions

Function ORACLE DB2 (LUW) Differences


Number to Character TO_CHAR CHAR - Oracle - > converts
Conversion number to a character with
no changes
- DB2 - > precedes the
number with zeroes.
Replace a string or a REPLACE REPLACE Not Applicable
word
Replace a Left most SUBSTR LEFT - Oracle - > Position to be
String mentioned
- DB2 positions on its own
to the left most string
Remove leading and TRIM TRIM - Oracle - > Removes
trailing characters characters from the left or
from a string right ends of a string or
both ends
- DB2 - > Removes leading

4
and trailing blank spaces
Number or a TO_NUMBER INTEGER or INT - Oracle - > function
Character conversion converts data of type char
to an integer or varchar2 to type number
and format provided
- DB2 - > converts either a
number or a valid character
value into an
Integer.
Remove trailing RTRIM RTRIM - Oracle - > Removes
characters from a characters from the right
string ends of a string or both
ends
- DB2 - > Removes trailing
blank spaces
Remove leading LTRIM LTRIM - Oracle - > Removes
characters from a characters from the left
string ends of a string or both
ends
- DB2 - > Removes leading
blank spaces
Number to a Currency TO_CHAR ------- - Oracle - > converts
format number to a currency
format provided in the
arguments
- DB2 - > NO BUILT IN
SIMPLE FUNCTION

Concatenate two CONCAT OR || || - Oracle - > Concatenates


strings two strings
- DB2 - > Concatenates two
strings

Conclusion

It is a reusable code and is generic to suit all versions of DB2 UDB LUW O/S. This
has been really helpful in the custom built reports for user PDA’s in my assignments.
I hope this utility would be helpful to tackle similar requirements on number
conversion in DB2 using SQL.

References
www.ibm.com

Vous aimerez peut-être aussi