|
View previous topic :: View next topic
|
| Author |
Message |
Rohit Umarjikar
Global Moderator

Joined: 21 Sep 2010 Posts: 3112 Location: NYC,USA
|
|
|
|
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 |
|
 |
Rohit Umarjikar
Global Moderator

Joined: 21 Sep 2010 Posts: 3112 Location: NYC,USA
|
|
|
|
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 |
|
 |
Rohit Umarjikar
Global Moderator

Joined: 21 Sep 2010 Posts: 3112 Location: NYC,USA
|
|
|
|
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 |
|
 |
|
|
 |
All times are GMT + 6 Hours |
|