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
 

 

Function-To find only starting Numbers from a String

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

Senior Member


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

PostPosted: Sat Sep 06, 2014 2:13 am    Post subject: Function-To find only starting Numbers from a String
Reply with quote

I have a column which contains Address and I need only starting number ( St Number alone ) , the expections is
e.g. input data
'123ABC'
Output
123

I have tried below query and it works fine with one restriction.

Code:
select Replace('123ABC',(Select Replace(Replace(Replace(Replace(Replace(Replace(Replace
        (Replace(Replace(Replace('123ABC','0',''),'1',''),'2',
        ''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9','')     
        from sysibm.sysdummy1 ) ,'') from sysibm.sysdummy1


Restriction:
If I get '123ABC1' then I do not need the last digit (1) but above query would return 1231 and I need just 123.

I know a UDF can be created for this but,I am looking for any SQL which does without a UDF.
Back to top
View user's profile Send private message

Rohit Umarjikar

Senior Member


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

PostPosted: Sat Sep 06, 2014 2:37 am    Post subject:
Reply with quote

I have tried below as well but could not be able get over the above restriction,

Code:
select cast(
        replace(
         translate('123ABC1',repeat('#',26),'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),'#','')as integer ) as Streetnum
from sysibm.sysdummy1
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


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

PostPosted: Sat Sep 06, 2014 2:56 am    Post subject:
Reply with quote

I learned to know lately that there will always be a space after the street number e.g. '123 ABC' so I tried below query and it is working fine,Thanks.

Code:
SELECT
rtrim(char(substr('123 ABC1',1,LOCATE(' ','123 ABC1'))))
from sysibm.sysdummy1
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 Incorrect output when trying to add n... monica1 PL/I & Assembler 10 Fri Jan 13, 2017 5:02 pm
No new posts find particular member name in PDS us... ravi243 CLIST & REXX 10 Mon Dec 19, 2016 6:44 pm
No new posts How to find the first monday of the w... abdulrafi COBOL Programming 10 Fri Nov 25, 2016 3:24 pm
This topic is locked: you cannot edit posts or make replies. RANDOM Function in COBOL swapnil781 COBOL Programming 2 Tue Nov 15, 2016 6:17 pm
No new posts Check System time(Minute) Using TIME1... balaji81_k DFSORT/ICETOOL 5 Fri Nov 11, 2016 10:53 am


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