Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Converting field from char to number

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
danylele74

New User


Joined: 03 Jul 2014
Posts: 27
Location: Italy

PostPosted: Tue Mar 29, 2016 4:24 pm    Post subject: Converting field from char to number
Reply with quote

Hi all,

i need to convert a field of a table from char to number with a SELECT statement.

Input field (char 20):

Code:
'7                   '
'24                  '
'155                 '
'3876                '
'74567               '


Output field

Code:
00007
00024
00155
03876
74567


I need ONLY the first five digits.

Thank you
Back to top
View user's profile Send private message

danylele74

New User


Joined: 03 Jul 2014
Posts: 27
Location: Italy

PostPosted: Tue Mar 29, 2016 4:52 pm    Post subject:
Reply with quote

I have found this:

Code:
SELECT RIGHT(REPEAT('0', 5) || RTRIM(CHAR(table_field)), 5)


it seems to work but only if the input field (CHAR) there are numbers, otherwise .....
Back to top
View user's profile Send private message
danylele74

New User


Joined: 03 Jul 2014
Posts: 27
Location: Italy

PostPosted: Tue Mar 29, 2016 4:55 pm    Post subject:
Reply with quote

The maximun output length is 5 (five) digits.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1219
Location: Israel

PostPosted: Tue Mar 29, 2016 4:58 pm    Post subject:
Reply with quote

If you are sure that you have only numbers and spaces in this column, you can try:
Code:
INT(YOUR_CHAR_COLUMN)

(Instead of INT you can use BIGINT or DEC).
In all cases, there are restrictions:
Quote:
The string must contain a valid string representation of a number.
Leading and trailing blanks are eliminated and the resulting string must conform to the rules for forming an integer constant.
Once it works, try:
Code:
DIGITS(INT(YOUR_CHAR_COLUMN))

and then
Code:
SUBSTR(DIGITS(INT(YOUR_CHAR_COLUMN)),6,5)
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1610
Location: NY,USA

PostPosted: Wed Mar 30, 2016 10:58 pm    Post subject:
Reply with quote

Also, this should give you what you want.
Code:
  select
LPAD(replace('7              ',' ',''),5,'0')
 from sysibm.sys1dummy
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Performing arithmetic on input field zh_lad DFSORT/ICETOOL 17 Tue Dec 06, 2016 8:04 pm
No new posts Add PD field from 2nd file to PD in 1st Sushant Garje DFSORT/ICETOOL 6 Thu Dec 01, 2016 4:32 pm
No new posts How to split the records using the am... vnktrrd DFSORT/ICETOOL 24 Fri Oct 28, 2016 7:33 pm
No new posts Comparing Decimal and CHAR columns rakesh17684 DB2 7 Thu Oct 20, 2016 2:33 am
No new posts Sort records based on numeric field. Alks SYNCSORT 2 Wed Oct 19, 2016 10:14 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us