MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to format BIGINT with Commas

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
RoyKMathur
Beginner


Joined: 05 Jan 2012
Posts: 47
Topics: 11

PostPosted: Thu May 22, 2014 6:12 am    Post subject: How to format BIGINT with Commas Reply with quote

I was wondering if there was a better way in DB2 z/OS to format a number with commas. In other words, how would you code a query to change a number from 3432149142 to 3,432,149,142 ?

I've developed a kludgy way of doing it, but was hoping for something more elegant. Currently it only handles numbers less than 10 billion.
Code:

CASE         WHEN TOTALROWS >= 1000000000             
  THEN CHAR(COALESCE(                                 
            SUBSTR(STRIP(CHAR(TOTALROWS)),1,1)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),2,3)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),5,3)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),8,3),'-1')) 
             WHEN TOTALROWS >= 100000000               
  THEN CHAR(COALESCE(                                 
            SUBSTR(STRIP(CHAR(TOTALROWS)),1,3)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),4,3)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),7,3),'-1')) 
             WHEN TOTALROWS >= 10000000               
  THEN CHAR(COALESCE(                                 
            SUBSTR(STRIP(CHAR(TOTALROWS)),1,2)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),3,3)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),6,3),'-1')) 
             WHEN TOTALROWS >= 1000000                 
  THEN CHAR(COALESCE(                                 
            SUBSTR(STRIP(CHAR(TOTALROWS)),1,1)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),2,3)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),5,3),'-1')) 
             WHEN TOTALROWS >= 100000                 
  THEN CHAR(COALESCE(                                 
            SUBSTR(STRIP(CHAR(TOTALROWS)),1,3)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),4,3),'-1')) 
             WHEN TOTALROWS >= 10000                   
  THEN CHAR(COALESCE(                                 
            SUBSTR(STRIP(CHAR(TOTALROWS)),1,2)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),3,3),'-1')) 
             WHEN TOTALROWS >= 1000                   
  THEN CHAR(COALESCE(                                 
            SUBSTR(STRIP(CHAR(TOTALROWS)),1,1)         
     ||','||SUBSTR(STRIP(CHAR(TOTALROWS)),2,3),'-1')) 
             ELSE CHAR(INTEGER(COALESCE(TOTALROWS,-1)))
END                                      AS TOTALROWS 
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12359
Topics: 75
Location: San Jose

PostPosted: Thu May 22, 2014 12:57 pm    Post subject: Reply with quote

RoyKMathur,

There is a simpler way to do it. How is TOTALROWS column defined in the table? Is it a Colum in the table or is it an output from a scalar function? I posted something similar here

http://www.mvsforums.com/helpboards/viewtopic.php?printertopic=1&t=10200&start=0&postdays=0&postorder=asc&vote=viewresult
_________________
Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort

www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12359
Topics: 75
Location: San Jose

PostPosted: Thu May 22, 2014 3:27 pm    Post subject: Reply with quote

Deleted as it does NOT handle negative numbers
_________________
Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort

www.linkedin.com/in/kolusu


Last edited by kolusu on Thu May 22, 2014 5:43 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12359
Topics: 75
Location: San Jose

PostPosted: Thu May 22, 2014 5:12 pm    Post subject: Reply with quote

bonk Just realized that with DB2 10 version it is quite simple

Code:

SELECT CHAR(TO_CHAR(TOTALROWS,'999,999,999,999,999,999'),24)   
  FROM TABLE
;

_________________
Kolusu - DFSORT Development Team (IBM)
DFSORT is on the Web at:
www.ibm.com/storage/dfsort

www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
RoyKMathur
Beginner


Joined: 05 Jan 2012
Posts: 47
Topics: 11

PostPosted: Tue May 27, 2014 5:35 am    Post subject: Reply with quote

That's fantastic!!! I'll spread this around my office. Thanks kolusu!!! Smile
Back to top
View user's profile Send private message
RoyKMathur
Beginner


Joined: 05 Jan 2012
Posts: 47
Topics: 11

PostPosted: Tue May 27, 2014 6:00 am    Post subject: Reply with quote

Before I posted I had spent time reviewing the manual and found nothing. I read right past TO_CHAR because it said it was for TIMESTAMPS... And because my number was a BIGINT, not a VARCHAR, I never even looked at VARCHAR_FORMAT. Thanks again! Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group