If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Simple Numeric Formatting

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-26-10, 14:57
WindInFace WindInFace is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
Simple Numeric Formatting

Hi,

With this example

SELECT 3000 MY_NBR FROM SYSIBM.SYSDUMMY1

How can I get the output of MY_NBR to include the comma thousands separator like 3,000?
Reply With Quote
  #2 (permalink)  
Old 04-26-10, 15:22
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
How you see the data is a function of what you are using, not DB2. If you are using the CLP, then numbers are just that--numbers. If you want a comma, then YOU need to convert it with some function (UDF).

Andy
Reply With Quote
  #3 (permalink)  
Old 04-26-10, 15:46
WindInFace WindInFace is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
Was that supposed to be helpful?
Reply With Quote
  #4 (permalink)  
Old 04-26-10, 16:55
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
DB2 has no mechanism built in that will format the number the way you want. If you want it displayed differently, then you have to convert it. I am sorry if you think this is not helpful, but that is the way it is.

Andy
Reply With Quote
  #5 (permalink)  
Old 04-26-10, 16:59
WindInFace WindInFace is offline
Registered User
 
Join Date: Mar 2010
Posts: 7
Thank you Andy, your second response was helpful!

Not what I wanted of course, but at least I know I can quit searching for a formatting function and create my own.
Reply With Quote
  #6 (permalink)  
Old 04-26-10, 17:11
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Arrow Nothing is new under Moon

Quote:
Originally Posted by WindInFace View Post
Hi,

With this example

SELECT 3000 MY_NBR FROM SYSIBM.SYSDUMMY1

How can I get the output of MY_NBR to include the comma thousands separator like 3,000?
You can find the answers on your question here:

Formatting the decimal string

Where I together with tonkuma made the different models of this process.

Lenny
Reply With Quote
  #7 (permalink)  
Old 04-27-10, 16:40
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation No recursive SQL at all

Solution without recursive SQL (you can use the part of the main SELECT, depending on the maximum length of your number):
Code:
select 
case length(integer_part)
     when 3
     then sign || strip(integer_part, l, '0') || fraction
     when 6
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ','  
               || substr(integer_part, 4, 3)  || fraction 
     when 9
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ','     
               || substr(integer_part, 7, 3)  || fraction
     when 12
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ','
               || substr(integer_part, 7, 3)  || ','      
               || substr(integer_part, 10, 3) || fraction
     when 15
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ','             
               || substr(integer_part, 13, 3) || fraction 
     when 18
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','                         
               || substr(integer_part, 16, 3) || fraction 
     when 21
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ','                                                
               || substr(integer_part, 19, 3) || fraction
     when 24
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','                                                                                               
               || substr(integer_part, 22, 3) || fraction  
     when 27
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','
               || substr(integer_part, 22, 3) || ','                                                                                                     
               || substr(integer_part, 25, 3) || fraction  
     when 30
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','
               || substr(integer_part, 22, 3) || ','              
               || substr(integer_part, 25, 3)|| ','                                                                                                     
               || substr(integer_part, 28, 3) || fraction 
     when 33
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','
               || substr(integer_part, 22, 3) || ','              
               || substr(integer_part, 25, 3) || ','
               || substr(integer_part, 28, 3) || ','
               || substr(integer_part, 31, 3) || fraction                                                                                              
end formatted_number

from (
select  
case when mod(length(integer_part), 3) >= 1
     then repeat('0', 3 - mod(length(integer_part), 3)) || integer_part
     else integer_part
end  integer_part, 
fraction, sign
from
(
select 
case when posstr(in_chr_nbr, '.') > 1 
     then strip(substr(in_chr_nbr, 1, posstr(in_chr_nbr, '.') - 1))
     else strip(in_chr_nbr)  
end integer_part,
case when posstr(in_chr_nbr, '.') > 1 
     then substr(in_chr_nbr, posstr(in_chr_nbr, '.')) 
     else ''
end  fraction,
sign

from 
(select 
case when input_nbr >= 0 
     then varchar(input_nbr) 
     else substr(varchar(input_nbr), 2)
end in_chr_nbr,
case when input_nbr >= 0 then ''
     else '-'
end sign
from (select -3000111222333444555666777888.999 input_nbr 
        from sysibm.sysdummy1) N1 ) N2 ) N3 ) N4
Result:

Quote:
FORMATTED_NUMBER
-3,000,111,222,333,444,555,666,777,888.999
Lenny
Reply With Quote
  #8 (permalink)  
Old 04-28-10, 13:09
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Thumbs up Improved formula. Doesn't use recursive SQL.

Improved formula doesn't use recursive SQL:

Code:
select  
case 
when integer_part <= '000' 
then sign || '0' || fraction
else
case
length(integer_part)
     when 3
     then sign || strip(integer_part, l, '0') || fraction
     when 6
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ','  
               || substr(integer_part, 4, 3)  || fraction 
     when 9
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ','     
               || substr(integer_part, 7, 3)  || fraction
     when 12
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ','
               || substr(integer_part, 7, 3)  || ','      
               || substr(integer_part, 10, 3) || fraction
     when 15
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ','             
               || substr(integer_part, 13, 3) || fraction 
     when 18
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','                         
               || substr(integer_part, 16, 3) || fraction 
     when 21
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ','                                                
               || substr(integer_part, 19, 3) || fraction
     when 24
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','                                                                                               
               || substr(integer_part, 22, 3) || fraction  
     when 27
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','
               || substr(integer_part, 22, 3) || ','                                                                                                     
               || substr(integer_part, 25, 3) || fraction  
     when 30
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','
               || substr(integer_part, 22, 3) || ','              
               || substr(integer_part, 25, 3) || ','                                                                                                     
               || substr(integer_part, 28, 3) || fraction 
     when 33
     then sign || strip(substr(integer_part, 1, 3), l, '0') || ',' 
               || substr(integer_part, 4, 3)  || ',' 
               || substr(integer_part, 7, 3)  || ',' 
               || substr(integer_part, 10, 3) || ',' 
               || substr(integer_part, 13, 3) || ','  
               || substr(integer_part, 16, 3) || ',' 
               || substr(integer_part, 19, 3) || ','
               || substr(integer_part, 22, 3) || ','              
               || substr(integer_part, 25, 3) || ','
               || substr(integer_part, 28, 3) || ','
               || substr(integer_part, 31, 3) || fraction                                                                                                                                                                                                                                                                                                                                       
 
end end formatted_number
from (
select  
case when mod(length(integer_part), 3) >= 1
     then repeat('0', 3 - mod(length(integer_part), 3)) || integer_part
     else integer_part
end  integer_part, 
fraction, sign
from
(
select 
case when posstr(in_chr_nbr, '.') > 1 
     then strip(substr(in_chr_nbr, 1, posstr(in_chr_nbr, '.') - 1))
     else strip(in_chr_nbr)  
end integer_part,
case when posstr(in_chr_nbr, '.') > 1 
     then strip(substr(in_chr_nbr, posstr(in_chr_nbr, '.')), t, '.') 
     else ''
end  fraction,
sign

from 
(select 
case when input_nbr >= 0 
     then varchar(0 + input_nbr) 
     else substr(varchar(0 + input_nbr), 2)
end in_chr_nbr,
case when input_nbr >= 0 then ''
     else '-'
end sign
from (select -3000111222333444555666777888.123 input_nbr 
        from sysibm.sysdummy1) N1 ) N2 ) N3 ) N4;
Result the same:

Quote:
FORMATTED_NUMBER
-3,000,111,222,333,444,555,666,777,888.123
Lenny
Reply With Quote
  #9 (permalink)  
Old 04-28-10, 16:42
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Alright Lenny let's see you show off now. How about in written format? For instance:
one million, three hundred ninety-five thousand, one hundred twenty-two.

Dave
Reply With Quote
  #10 (permalink)  
Old 04-28-10, 17:03
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation You can do it

Quote:
Originally Posted by dav1mo View Post
Alright Lenny let's see you show off now. How about in written format? For instance:
one million, three hundred ninety-five thousand, one hundred twenty-two.

Dave
How you know that's easy !

Lenny
Reply With Quote
  #11 (permalink)  
Old 04-28-10, 23:05
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Wink -3,000,111,222,333,444,555,666,777,888.123

Quote:
Originally Posted by dav1mo View Post
Alright Lenny let's see you show off now. How about in written format? For instance:
one million, three hundred ninety-five thousand, one hundred twenty-two.

Dave
Can you read the number -3,000,111,222,333,444,555,666,777,888.123 for me ?

Thank you. Kara.

Last edited by DB2Plus; 04-28-10 at 23:08.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On