substring

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
poorna_76
Charter Member
Charter Member
Posts: 190
Joined: Thu Jul 08, 2004 10:42 am

substring

Post by poorna_76 »

Hi All,

I would like to extract specific substring from string, without knowing the position of that substring.
That particular substring position is not fixed, it can be any where in the String.


Example:This Job Extracts New Employment Data for the Processing month Based on Current Month File

I would like to extract "Employment" from the above string.

Any thoughts?

Thanks in Advance.
rwierdsm
Premium Member
Premium Member
Posts: 209
Joined: Fri Jan 09, 2004 1:14 pm
Location: Toronto, Canada
Contact:

Re: substring

Post by rwierdsm »

Poorna,

Are you trying to test for the presence of the string? Or perhaps the location of the string?

You could do:

Code: Select all

MyString = "This Job Extracts New Employment Data for the Processing month Based on Current Month File" 

Position = Index(MyString, 'Employment', 1)
This would return the position of the string 'Employment'. If you check for return > 0 then it tells you if your search string is present anywhere in the string.

HTH

Rob W.
Rob Wierdsma
Toronto, Canada
bartonbishop.com
Sunshine2323
Charter Member
Charter Member
Posts: 130
Joined: Mon Sep 06, 2004 3:05 am
Location: Dubai,UAE

Post by Sunshine2323 »

Hi Poorna,

If I understand your requirement then you want to extract a string from the input in which the location of the string to be extracted is not fixed.

For this you need to know the Length of the string to be extracted and its location in the input string

Declare StageVariables:

StgLen:Len(ExtractWord)
StgPosition:Index(Input.Sentence, ExtractWord, 1)
StgExtract:Substrings(Input.Sentence, StgPosition, StgLen)

ExtractWord: Is the string you want to extract from the input

StgExtract holds the word you want to extract.

Hope this helps :)
Warm Regards,
Amruta Bandekar

<b>If A equals success, then the formula is: A = X + Y + Z, X is work. Y is play. Z is keep your mouth shut. </b>
--Albert Einstein
kumar_s
Charter Member
Charter Member
Posts: 5245
Joined: Thu Jun 16, 2005 11:00 pm

Post by kumar_s »

:lol: You already got the substring... What else you want from the main string.
I guess you are missing out someting.
Is it you want to know the position of where the substring present or to know the presence of the substring in the main string?
Impossible doesn't mean 'it is not possible' actually means... 'NOBODY HAS DONE IT SO FAR'
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Re: substring

Post by chulett »

poorna_76 wrote:I would like to extract specific substring from string, without knowing the position of that substring. That particular substring position is not fixed, it can be any where in the String.
Technically, you will have to know or determine the position of the substring if you truly want to extract it.

However, that being said - if the substring to extract is known ahead of time - aka 'specific' as yu've noted - then (as Kumar pointed out) you don't really need to extract it, do you? :wink:

All you need to do is verify the existance of the sub-string within the string in question, and Rob has already shown you how to do that with the Index function. All I would change were I to do it is the variable name 'Position' to something I would treat as a boolean - like 'SubstringExists'. Then you can use it in an If-Then-Else construct in a manner that is easy to grok:

Code: Select all

MyString = "This Job Extracts New Employment Data for the Processing month Based on Current Month File" 

SubstringExists = Index(MyString, 'Employment', 1)

If SubstringExists Then [do something] End Else [do something else] End
To expand on what Rob posted. This works because 'false' is a zero and 'true' is any non-zero value. The system variables @FALSE and @TRUE are zero and one, respectively, so while you could have directly compared Position to @FALSE, doing the same with @TRUE would only have worked in one very specific scenario.

If you aren't planning on evaluating the result of this more than once, there's no need to store the intermediate result. A little less readable but perfectly fine:

Code: Select all

MyString = "This Job Extracts New Employment Data for the Processing month Based on Current Month File" 

If Index(MyString, 'Employment', 1) Then [do something] End Else [do something else] End
Hope that helps,
-craig

"You can never have too many knives" -- Logan Nine Fingers
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Sunshine, just an FYI. If you wanted to go crazy you could shorten that all down to one expression:

Code: Select all

Substrings(Input.Sentence,Index(Input.Sentence,ExtractWord,1),Len(ExtractWord))
I'm sure you broke it up to illustrate the individual steps, but also wanted to point out it could all be 'inlined' and thus used in a Transform if someone wanted to. :wink:

Of course, you'd need to check the result to see if it extracted anything. I haven't tested it but pretty sure it will return a NULL if the substring does not exist within the string.
-craig

"You can never have too many knives" -- Logan Nine Fingers
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

It will return the original string. If 0 is used as the the argument in Substrings(), it is replaced with 1. It's also documented thus in the DataStage BASIC manual.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Ah. Thanks for the clarification, Mr Ray. :D
-craig

"You can never have too many knives" -- Logan Nine Fingers
Post Reply