IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Formatting the decimal string


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Sat Sep 12, 2009 7:23 pm
Reply with quote

The following query give you the easiest way to format the Decimal Number to string:

Code:
with input (number) as
(select -1123167890.4327  from sysibm.sysdummy1
)
,
transform_1 (num, part, rem, trans, fract, k ) as
(select number, int(0), int(abs(number)), varchar('', 150),
        abs(number) - int(abs(number)), 12
  from input where abs(number) >= 1000   
union all
select
number, int(rem / power(10, k - 3)) , mod(rem, power(10, k - 3)),
strip(case when  int(rem / power(10, k - 3)) = 0 and trans <= ' ' then  ''
           when  int(rem / power(10, k - 3)) > 0 and trans <= ' ' then
                 case when number < 0 then '-' else '' end
              || strip(digits(int(rem / power(10, k - 3))), l, '0')
            else trans || ',' || substr(digits(int(rem / power(10, k - 3))), 8, 3)
       end
       || case when k - 3 = 0 and fract > 0 then strip(varchar(fract), l, '0') else '' end, l, '0')
, fract, k - 3
  from transform_1, input 
where k - 3 >= 0 and abs(number) >= 1000 
union all
select number, int(0), int(0), varchar(number), number, 0 
from input
where abs(number) < 1000 
)
select num "Input",
         trans "Result"
  from transform_1 tr where k = 0


Quote:
Input............................ Result
-1123167890.4327 -1,123,167,890.4327


Thanks, Lenny icon_arrow.gif icon_exclaim.gif icon_arrow.gif
Back to top
View user's profile Send private message
lkhiger

New User


Joined: 28 Oct 2005
Posts: 89

PostPosted: Sat Sep 12, 2009 7:30 pm
Reply with quote

DB2 V9 supports
VARCHAR_FORMAT ( decimal-floating-point-expression [, format-string] ).

All users of V9 can use my query for learning what inside.

Any others, which doesn't have V9, yet, can make UDF based on this query.

Lenny icon_exclaim.gif icon_idea.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
No new posts Need help on formatting a report DFSORT/ICETOOL 14
Search our Forums:

Back to Top