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 > db2-- Decimal to Varchar without leading zeros

Reply
 
LinkBack Thread Tools Display Modes
  #31 (permalink)  
Old 08-11-09, 06:35
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Quote:
Originally Posted by Peter.Vanroose
Indeed, at least, for strictly positive integers.
For the number 0, it's giving an unwanted empty string...
See the message from Lenny77. It's working without any problem.

K.S
Reply With Quote
  #32 (permalink)  
Old 04-08-10, 09:16
livefaq livefaq is offline
Registered User
 
Join Date: Apr 2010
Posts: 1
I am surffing for a while in internet to solve decimal conversion problem finally i found a website which is very effective
to solve decimal to fraction conversion problem.. i am sharing you guys my discovery here is a URL of the website Decimal to fraction,Deciamls to fractions,decimal to fractions
Reply With Quote
  #33 (permalink)  
Old 04-08-10, 10:16
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,053
Actually, that's the well-known continued fraction expansion algorithm. (I looked into this for my Master thesis, very long ago ;-)

But that was not the original question of this thread, I believe...
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #34 (permalink)  
Old 11-25-10, 14:41
sandydaufen sandydaufen is offline
Registered User
 
Join Date: Nov 2010
Location: CA
Posts: 3
thanks for the work around, might be useful in the future
Reply With Quote
  #35 (permalink)  
Old 01-04-11, 18:26
MatthewNeill MatthewNeill is offline
Registered User
 
Join Date: Jan 2011
Location: Phoenix
Posts: 4
Trim Leading '0' without removing last '0'...

An IfNull NullIf combination can make the Trim Leading '0' example workable even for a true '0' only value.

Code:

VarChar(
   IfNull(
      NullIf(
         Trim(Leading '0' From 
            Trim(
               Char( -000010 )
            )
         )
      ,'')
   ,'0')
)

Will result in '-10'

Just replace -000010 with your numeric column...

Regards...
Reply With Quote
  #36 (permalink)  
Old 01-04-11, 19:06
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 816
Exclamation

You are almost create the nuclear bomb from the scratch:

Code:
select varchar(int('-000010'))
from sysibm.sysdummy1
;
Lenny
Reply With Quote
  #37 (permalink)  
Old 01-04-11, 19:19
MatthewNeill MatthewNeill is offline
Registered User
 
Join Date: Jan 2011
Location: Phoenix
Posts: 4
Decimal (not int) to Varchar without leading zeros...

The heading of this thread is "Decimal to Varchar without leading zeros" so I assume that there may be digits to the right of the decimal point that would want to be returned...

Code:
select varchar(int('-010.10'))
from sysibm/sysdummy1
yeilds '-10' instead of '-10.10',


whereas my bomb:

Code:
Select 
VarChar(
   IfNull(
      NullIf(
         Trim(Leading '0' From 
            Trim(
               Char( -010.10 )
            )
         )
      ,'')
   ,'0')
)
from sysibm/sysdummy1

has a (10.10 megaton) yeild of '-10.10' like it should...

Last edited by MatthewNeill; 01-04-11 at 19:22.
Reply With Quote
  #38 (permalink)  
Old 01-05-11, 12:45
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 816
Thumbs up Easier is better

Believe me, I like your query very much, and I am understanding how it works, but people like something easier looking.

For example:

Code:
select    strip(strip(left(decChar, 1), L, '0'), L, '+') 
       || strip(strip(substr(decChar, 2)), L, '0') chardec
from (
Select strip('   -  000077.77') as decChar
from sysibm.sysdummy1 ) t1
Returns:

Quote:
CHARDEC
-77.77
In my opinion, even beginner DB2 programmer can create something like this query....

Lenny

Last edited by Lenny77; 01-05-11 at 12:51.
Reply With Quote
  #39 (permalink)  
Old 01-05-11, 13:13
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 816
Question BOMB was not explode

Quote:
Originally Posted by MatthewNeill View Post
An IfNull NullIf combination can make the Trim Leading '0' example workable even for a true '0' only value.

Code:

VarChar(
   IfNull(
      NullIf(
         Trim(Leading '0' From 
            Trim(
               Char( -000010 )
            )
         )
      ,'')
   ,'0')
)

Will result in '-10'

Just replace -000010 with your numeric column...

Regards...
I just copy your query and run:

Code:
select VarChar(
   IfNull(
      NullIf(
         Trim(Leading '0' From 
            Trim(
               Char( -000010 )
            )
         )
      ,'')
   ,'0')
)
from sysibm.sysdummy1
And what I have instead of result:

SQL0104N An unexpected token "'0'" was found following "". Expected tokens may include: "CONCAT || / MICROSECONDS MICROSECOND SECONDS SECOND MINUTES". SQLSTATE=42601

State:42601,Native:-104,Origin:[IBM][CLI Driver][DB2]


Please correct your query and publish again.

Your BOMB was not explode

Lenny
Reply With Quote
  #40 (permalink)  
Old 01-05-11, 13:28
MatthewNeill MatthewNeill is offline
Registered User
 
Join Date: Jan 2011
Location: Phoenix
Posts: 4
I guess that 'easier looking' is in the eye of the beholder...

That is so funny, because the only reason I posted the "Trim Leading Zero" reimplementation with the IfNull-NullIf was because if thought that it WAS easier to understand then the case-cast-abs-strip-cast-substr-cast... implementation...

How about a compromise... Your CASE for my NULLS and my TRIM LEADING for your STRIPS and SUBSTR...

Code:
Select 

case when dec_val = 0 then '0'
else Trim(Leading '0' From Char( dec_val ))
end as zero_suppressed_decimal_string

from (select -010.10 dec_val from sysibm/sysdummy1) as t1

Code by Committee~!

Cheers!
Reply With Quote
  #41 (permalink)  
Old 01-05-11, 13:33
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 816
what DB2 system are you using ?
where sysibm/sysdummy1 is using instead of sysibm.sysdummy1 ?
Reply With Quote
  #42 (permalink)  
Old 01-05-11, 14:27
MatthewNeill MatthewNeill is offline
Registered User
 
Join Date: Jan 2011
Location: Phoenix
Posts: 4
DB2 for i

Sorry I should have change the slash notation back to dot notation...

DB2 for i (the former DB2/400) is the third major incarnation of DB2. It is very closely incorporated into the operating system of the IBM System i machines...

A.K.A., "IBM Power Systems"...
A.A.K.A., "i5"...
A.A.A.K.A., "iSeries"...
A.A.A.A.K.A., "AS/400"...
A.A.A.A.A.K.A., Silverlake...

Matthew...
Reply With Quote
  #43 (permalink)  
Old 06-23-11, 13:58
dorje dorje is offline
Registered User
 
Join Date: Jun 2011
Posts: 1
Lightbulb What about this one?

Well, maybe there are simpler ways, but I wrote this, and seems to work well...

RTRIM(char(integer(<DOUBLE_VALUE>))) ||'.'||right('00000'||trim(char(integer(round((abs(<DOUBLE_VALUE>)-trunc(abs(<DOUBLE_VALUE>),0))*10000,0)))),4)

The underlined values might needed to change if 4 decimal digits is not the choice you need...

Take care!
István
Reply With Quote
Reply

Thread Tools
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