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 > How to convert INT 0 or 10 to 001

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-05-09, 10:45
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
How to convert INT 0 or 10 to 001

Thanks Thanks Thanks Thanks Thanks
Reply With Quote
  #2 (permalink)  
Old 11-05-09, 11:15
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Right(Digits(your_number), 3) ....

Lenny
Reply With Quote
  #3 (permalink)  
Old 11-05-09, 11:40
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
Great! Thanks

I used CAST(mycolumn as CHAR(3)) and it didn't work.

Thanks
Reply With Quote
  #4 (permalink)  
Old 11-06-09, 05:05
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What do you mean with "didn't work"?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 11-06-09, 09:17
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
select cast (1 as char(3))

result: 1, not 001
Reply With Quote
  #6 (permalink)  
Old 11-06-09, 09:55
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Well, it returns a string with 3 characters where the first 2 are blanks. So you have to replace those blacks with 0's.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 11-06-09, 10:23
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
So what's the syntax if using cast to do that?
Reply With Quote
  #8 (permalink)  
Old 11-06-09, 10:51
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question

Does this not work ?

Code:
Select   Right(Digits(your_number), 3)
  from   your_table
Lenny
Reply With Quote
  #9 (permalink)  
Old 11-06-09, 14:06
resuwen resuwen is offline
Registered User
 
Join Date: Apr 2009
Location: San Jose, CA
Posts: 1
The CAST will left justify. e.g. cast(1 as char(3)) will give you '1 '
I am not sure what your exact question is.
Are you trying to have leading zeros for any int number or just for a 3 digit number?
Do you need to handle negative numbers?
Do you need to handle INT column only or decimal/smallint/ect. as well?
For instance:

1 => 001
10 => 010
100 => 100
1000 => ?
-1 => ?
1.0 => ?


If you ever only need up to 3 digits for INT, and do not have negative numbers, you can do:
SUBSTR( DIGITS(int_number), 8)
e.g. select substr(digits(1), 8) from sysibm.sysdummy1 => '001'


--Andrei L.
Reply With Quote
  #10 (permalink)  
Old 11-06-09, 15:51
newer newer is offline
Registered User
 
Join Date: Sep 2003
Posts: 32
No, Right(Digits(your_number), 3)
is good for me.

I only have INT and no negative. I thought Cast(1 as char(3)) will give me 003, now I know it is not.

Thanks,
Reply With Quote
  #11 (permalink)  
Old 11-06-09, 15:55
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by newer View Post
No, Right(Digits(your_number), 3)
is good for me.

I only have INT and no negative. I thought Cast(1 as char(3)) will give me 003, now I know it is not.

Thanks,
Sounds good for me.

Lenny
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