IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Select numeric portion from CHAR datatype column


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
balaji81_k

Active User


Joined: 29 Jun 2005
Posts: 155

PostPosted: Sat Aug 19, 2017 1:51 am
Reply with quote

Hi Friends,

I am trying select a numeric portion from char field which is of length 10 .
I am trying to use cast and substr together but it is giving error .
Can you please help and advise how we can achieve the numeric portion as integer from SQL .

OPER is char (10) and it hold value like 'OPS01234 ' , need to retreive 01234 as int with length of 5 bytes.

Code:

SELECT CAST(SUBSTR(OPER,4,5) AS INTEGER)
FROM TEST_TABLE

got SQL error =  -420 "Value of string argument was not acceptable to
sysibm.integer function


can any one help me to overcome this error.
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 446
Location: USA

PostPosted: Sat Aug 19, 2017 3:08 am
Reply with quote

If the value of SUBSTR(OPER,4,5) is numeric, then you'll Not get any error.

But, if the SUBSTR(OPER,4,5) contains any non-numeric value then you'll get sqlcode -420.

So, you may be getting a -420 for a value that is Not OPS01234.

Check all the values you are getting with SUBSTR(OPER,4,5) i.e. don't CAST yet and see the values to find any non-numeric data.

.
Back to top
View user's profile Send private message
balaji81_k

Active User


Joined: 29 Jun 2005
Posts: 155

PostPosted: Mon Aug 21, 2017 9:49 pm
Reply with quote

RahulG31 wrote:
If the value of SUBSTR(OPER,4,5) is numeric, then you'll Not get any error.

But, if the SUBSTR(OPER,4,5) contains any non-numeric value then you'll get sqlcode -420.

So, you may be getting a -420 for a value that is Not OPS01234.

Check all the values you are getting with SUBSTR(OPER,4,5) i.e. don't CAST yet and see the values to find any non-numeric data.

.


Sure Rahul , i will check in this.
Back to top
View user's profile Send private message
balaji81_k

Active User


Joined: 29 Jun 2005
Posts: 155

PostPosted: Tue Aug 22, 2017 2:42 am
Reply with quote

Hi Rahul,
It works good for me .Thanks icon_smile.gif
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 446
Location: USA

PostPosted: Tue Aug 22, 2017 2:56 am
Reply with quote

balaji81_k wrote:
Hi Rahul,
It works good for me .Thanks :)

So, that means that you were able to find the non-numeric data in SUBSTR(OPER,4,5) and that was causing sqlcode -420, right?

.
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3051
Location: NYC,USA

PostPosted: Tue Aug 22, 2017 3:18 am
Reply with quote

Why Hardcode? if the position changes in future then what?
Code:
select
replace(RTRIM(TRANSLATE(upper('OPS01234 '), ' ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')),' ','' )
END from sysibm.sysdummy1
Back to top
View user's profile Send private message
balaji81_k

Active User


Joined: 29 Jun 2005
Posts: 155

PostPosted: Wed Aug 23, 2017 9:31 pm
Reply with quote

Rohit Umarjikar wrote:
Why Hardcode? if the position changes in future then what?
Code:
select
replace(RTRIM(TRANSLATE(upper('OPS01234 '), ' ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')),' ','' )
END from sysibm.sysdummy1


Many thanks Rohit for the suggestion.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts How to load to DB2 with column level ... DB2 6
No new posts RC query -Time column CA Products 3
Search our Forums:

Back to Top