Page 1 of 1

How to find first occurance of numeric value in a string

Posted: Fri Dec 02, 2011 4:51 pm
by horserider
I have a string VARCHAR[10]. Here is what is needed

(1) First find out if a string has any numeric digits or not.
(2) If yes, find the first position of the numeric digit.
(3) Also, find what that numeric digit is.

I can find out if a string has numeric or not using Alpha() but for those string that tells me that they do contains some numeric value, how do I find #2 and #3mentioned above?

Posted: Fri Dec 02, 2011 8:07 pm
by Mike
Interesting requirement...

Off the top of my head:
1) Custom routine
2) Brute force using 10 index functions and some if-then-else logic
3) Get creative with length and convert functions

If this is a 1-time deal, then I'd just go the brute force route. If it will be a frequent requirement, then I'd go with the custom routine.

Mike

Posted: Fri Dec 02, 2011 8:10 pm
by horserider
Even if I plan to use search function (INDEX) 10 times, how to I search for numbers 0-9 in a string. Can this be done in 1 function?

Posted: Fri Dec 02, 2011 8:34 pm
by qt_ky
You can do it in a Transformer stage, something like this (creative way):

Code: Select all

lnk.col[Index(Convert('1234567890', '##########', lnk.col), '#', 1), 1]
If '#' will ever exist in the input column, then use some other character that is unique instead.

Posted: Sat Dec 03, 2011 3:59 am
by ray.wurlod
If you're prepared to use a BASIC Transformer stage or server job, you could have

Code: Select all

MatchField(InLink.TheString,"0X1N0X",2)
which would answer all three questions.

Posted: Wed Mar 28, 2012 4:36 am
by karthi_gana
I am really curious to know the meaning of 0X1N0X

Posted: Wed Mar 28, 2012 4:40 am
by karthi_gana
I have also seen this on

http://www.pickwiki.com/cgi-bin/wiki.pl ... AMVedit_AE

it seems like BASIC SUBROTUINE written in the above link.

Posted: Wed Mar 28, 2012 7:22 am
by chulett
It's a pattern, the UniVerse equivalent of a regular expression. The MATCH operator is fully explained in the UniVerse BASIC pdf. I don't believe in posting entire chunks of manuals here, so suffice it to say it matches any number of any characters followed by one numeric character followed by any number of any characters. 'Any' can also include 'none', btw.

Posted: Wed Mar 28, 2012 3:25 pm
by ray.wurlod
You can read about DataStage BASIC match patterns in, curiously, the DataStage BASIC manual.
:shock:

Posted: Wed Mar 28, 2012 3:26 pm
by chulett
That too. :wink:

Posted: Wed Mar 28, 2012 8:55 pm
by horserider
Index and Convert as proposed worked just fine.

Posted: Tue Jun 05, 2012 5:54 am
by shobhit_vk_gupta
A simple solution which I can suggest but it is in Server Job:

Let Say:

The column name in which you have to perform this logic is X.

In transformer of Server Job you can write the below mentioned transformation:

Oconv(X,'MCN')[1,1]

Above written Oconv function will give all the numerals as a output and then we are picking the first one.

Let me know if it servers the purpose.

Posted: Tue Jun 05, 2012 6:33 am
by chulett
FYI - the DIGITS transform is a short-cut to that particular OConv function.

Posted: Tue Jun 05, 2012 10:06 am
by rameshrr3
The OConv will answer everything except the position , so an Index needs to be used along with it , probably in a different transformer derivation ( Assuming this is for a transformer ! )

Code: Select all

Index(InputString,Oconv(InputString,"MCN")[1,1],1)