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 > Double to Varchar

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-11-07, 09:59
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
Double to Varchar

Hello Gurus,

I have written a DB2 procedure which calculates some values using a formula and the result of the formula which should be and is a double value. Now my problem here is that i want to save this value in a table field which is varchar datatype. when i use the following

cast(cast(xFInterTotal as char(250)) as varchar(1024))

and cast it into varchar it saves the value in the varchar field, but this is not what i want, it saves the value in a uninterpreted form (1.104E4) where the actual double value is 4800.
How can i save 4800 as is in the varchar field. Please guide me in doing this.

Thanks and Regards
Harish
Reply With Quote
  #2 (permalink)  
Old 09-11-07, 16:56
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Cast the double to decimal first.

Andy
Reply With Quote
  #3 (permalink)  
Old 09-12-07, 01:53
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
Thanks Andy

Tried that but still it is giving me different result

In the varchar field it saves 11040.
where as in the double field it saves 4800

even though the calulation is same for both the values. I am not sure how this can be done.....Here is what i am doing whicle casting it to varchar

insert into Test_Proc values (xid, cast(cast(cast(xFInterTotal as decimal) as char(250)) as varchar(1024)), xResourceRate, 'xResourceRate')

here the second field is the varchar field and the third field is a double field.

Is there any other way to do this
Reply With Quote
  #4 (permalink)  
Old 09-12-07, 03:51
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Could you provide is with a small but full example where you get the wrong values? I guess that you have some other code that changes the value and is responsible for the differences.

Here you have an example how the output looks like for some casts:
Code:
$ db2 "values ( double(4800), decimal(double(4800), 8, 2), char(double(4800)), char(decimal(double(4800), 8, 2)) )"

1                        2          3                        4
------------------------ ---------- ------------------------ ----------
  +4.80000000000000E+003    4800.00 4.8E3                    004800.00
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 09-12-07, 05:26
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
Hi

I tried the following...

varchar(char(decimal(double(xResourceRate))))

and the double value of xResourceRate = 6240 and in the varchar field it saves the following : 000000000006240.

this is near to then the last time, but this is not the exact.
Reply With Quote
  #6 (permalink)  
Old 09-12-07, 07:52
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Quote:
Originally Posted by donraja_ht
Hi

I tried the following...

varchar(char(decimal(double(xResourceRate))))

and the double value of xResourceRate = 6240 and in the varchar field it saves the following : 000000000006240.

this is near to then the last time, but this is not the exact.
What datatype is xResourceRate?

Andy
Reply With Quote
  #7 (permalink)  
Old 09-12-07, 08:58
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
It is double i presume, the query for calculation goes here

declare xResourceRate DOUBLE;
select sum(COALESCE(resource_rate, 0) * xduration / xCount) into xResourceRate from table_assignment;


Thanks in advance
Reply With Quote
  #8 (permalink)  
Old 09-12-07, 09:25
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
I am afraid that that is the best DB2 is going to do for you. Your only alternative is to write a UDF that removes the leading zeros.

Andy
Reply With Quote
  #9 (permalink)  
Old 09-12-07, 12:50
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
Thanks Andy for your Help :-) will have to look for a alternative solution
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