cancel
Showing results for 
Search instead for 
Did you mean: 

Finding a variable in a text field

Former Member
0 Kudos

I have text field data that looks like this:

RE-WORKX10/ZSB/Q2/ZONS/Q2/ZPM/Q7/

I am trying to pull the number after the X out up to /

Any suggestions

tx

Shirley

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Try this...


Mid({TableName.StringField}, Instr({TableName.StringField},'X')+1, 
(InStr({TableName.StringField},'/') - InStr({TableName.StringField},'X') -1))

HTH,

Jason

Former Member
0 Kudos

Jason will that work even if the x is the first variable?

Edited by: Shirley Cunningham on May 26, 2010 5:42 PM

Former Member
0 Kudos

X isn't a variable. It's being used as a marker to indicate where you want to extracting text out of your string...

The supplied formula uses the Mid function to pull a section of text out of a string


Mid({your string field here}, start position, length of text to extract)

The 1st InStr function is used to find the 1st "X" in the string. The "+1" moves the pointer over 1 character, so that the X itself is not included.


Instr({TableName.StringField},'X')+1

The last part of the formula...


(InStr({TableName.StringField},'/') - InStr({TableName.StringField},'X') -1)

counts the number of characters between the 1st "X" and the 1st "/"... Thus supplying the Mid function the correct number of characters to extract.

So, based on the example to provided, the posted formula will return a value of 10.

Former Member
0 Kudos

No. You would have to modify the formula starting and ending characters for the characters that frames the text you are looking to extract from the string.

Former Member
0 Kudos

Hi Jason,

I'm getting an error "String Length is less than 0 or not an integer" I have added the

if Instr({@Desc- Lower Case},'x')< 1 THEN '' to the original formula you supplied however I'm still getting the error.

The Formula giving the ERROR:

if Instr({@Desc- Lower Case},'x')< 1 THEN '' else

if (Instr({@Desc- Lower Case},'x')+1)IN [1 TO 9] THEN

Mid ({@Desc- Lower Case}, Instr({@Desc- Lower Case},'x')+1,

(InStr({@Desc- Lower Case},'/') - InStr({@Desc- Lower Case},'x') -1))

Data field examples:

fixed LB WO 0006254

Fixed sc/x5/zsb/q2/u5/w3

split/solder

x2/zpwv/q5/r8

I believe if I could narrow the field selection down to those records that have a number from 1 to 9 following the X I could create some consistency in the field? The above formula was the only thing I could come up with, but I am stll getting an error starting with Mid.

Any help would be greatly appreciated!!!!!

Answers (3)

Answers (3)

Former Member
0 Kudos

That work Sanjay. Thank you

Former Member
0 Kudos

Getting error from prior formula

Former Member
0 Kudos

May I suggest an alternate formula which splits the string into pieces based on the existence of '/' and then evaluates each piece to see if 'x' exists and if the next character is numeric then display the text next to 'x' else blank.

For example:


// X is the string field
stringvar array a := split(x,'/');
stringvar z := '';

numbervar i;

for i := 1 to count(split(x,'/')) do
(
if instr(a<i>,'x') > 0 and a<i>[instr(a<i>,'x')+1] in '0' to '9' then
(
z := z & a<i>[instr(a<i>,'x')+1 to length(a<i>)];
exit for;
));

z;

If the data string happens to be a2/zpwv/q5/r8/x123x5 then it will return 123x5. Also, once it finds a matching condition it will quit and will not process the string further, if you want it to continue processing, just comment out the EXIT FOR statement.

For example, if the string is x2/a2/zpwv/q5/r8/x123x5 the result will be 2.

Edited by: Sanjay Kodidine on Jun 1, 2010 2:25 PM

Former Member
0 Kudos

Thank you for the input, I'm learning but not enough to find the error in this formula. I am getting a bracket error at the second " instr(ai],'x')" .

What does the [ bracket represent?

Will I need to create individual formulas for each split or does this formula evaluate all splits and return only the one with the number next to it?

// X is the string field

stringvar array a := split({@Des-Data Extraction},'/');

stringvar z := '';

numbervar i;

for i := 1 to count(split({@Des-Data Extraction},'/')) do

(

if instr(a<i>,'x') > 0 and a<i>[instr(ai],'x')+1 in '0' to '9' then

(

z := z & a<i>[instr(a<i>,'x')+1 to length(ai]);

exit for;

));

z;

Shirley

Former Member
0 Kudos

Shirley:

Just copy the following as is into a formula and run it. I do not get any errors when I run my original formula. The error comes into play when you substitute X with {@Des-Data Extraction} in some places only.


evaluateafter({@Des-Data Extraction});

stringvar x := {@Des-Data Extraction};
stringvar array a := split(x,'/');
stringvar z := '';
numbervar i;
 
for i := 1 to count(split(x,'/')) do
(
if instr(a<i>,'x') > 0 and a<i>[instr(a<i>,'x')+1] in '0' to '9' then
(
z := z & a<i>[instr(a<i>,'x')+1 to length(a<i>)];
exit for;
));
 
z;

This formula will evaluate all splits and return only the one with the number next to it (if any).

Edited by: Sanjay Kodidine on Jun 2, 2010 7:56 AM

Former Member
0 Kudos

Sanjay,

I copied it, and I am still getting an error that it is missing a ) at the location I stated above. I am using crystal syntax and Crystal version 10.5

Former Member
0 Kudos

If you have copied the formula exactly as is and not made any changes, then I am not sure why it is giving errors, unless CR 10.5 cannot handle array manipulation. I ran the formula in CR 2008, CR XI R1 and CR XI R2 and it works just fine without any errors. I have no way of testing it in CR 10.5.

Can anyone else confirm the limitation of CR 10.5?

If that is the case, then we are back to square one. Let me see if I can re-work the original formula, unless someone beats me to it

Former Member
0 Kudos

Sanjay,

I have a working formula that pulls the number to the right of the x already. The problem is users are enter comments before the x such as words like fixed.

fixed sc x3/zsb/q1/u5

All I really need is something that will look for the first instance of an X with a Number after it and then return everything to the right of it. I just want to eliminate anything before that X3.

x3/zsb/q1/u5

Former Member
0 Kudos

OK here is a modified formula without any arrays. It has limitations in that it will process up to two 'x's being present in the string. For example: fixed sc x1113/zsb/q1/u5 will work fine or one without any X but if the string has 3 Xs then it wont work unless the ELSE part is modified.


stringvar s := {@Des-Data Extraction};
if instr(s,'x') > 0 and mid(s,instr(s,'x')+1,1) in '0' to '9' then
Mid (s, Instr(s,'x')+1,(InStr(s,'/') - InStr(s,'x') -1))
else
(
s := replace(s,'x','.',1,1);
if instr(s,'x') > 0 and mid(s,instr(s,'x')+1,1) in '0' to '9' then
Mid (s, Instr(s,'x')+1,(InStr(s,'/') - InStr(s,'x') -1));
);

Edited by: Sanjay Kodidine on Jun 3, 2010 4:00 PM

Former Member
0 Kudos

The formula saves with no errors, but I'm getting the same error I got with my original formula which is :

"String Length is less than 0 or not an integer"

it brings me back to this section of the formula

Mid (s, Instr(s,'x')+1,(InStr(s,'/') - InStr(s,'x') -1))

Former Member
0 Kudos

stringvar s := {@Des-Data Extraction};
//
if instr(s,'x') > 0 and mid(s,instr(s,'x')+1,1) in '0' to '9' 
and instr(mid(s,instr(s,'x')+1,length(s)),'/') <= 0 then
Mid (s, Instr(s,'x')+1,length(s))
else
if instr(s,'x') > 0 and mid(s,instr(s,'x')+1,1) in '0' to '9' then
Mid (s, Instr(s,'x')+1,(InStr(mid(s,instr(s,'x')+1,length(s)),'/')-1))
else
(
s := replace(s,'x','.',1,1);
if instr(s,'x') > 0 and mid(s,instr(s,'x')+1,1) in '0' to '9' then
Mid (s, Instr(s,'x')+1,(InStr(mid(s,instr(s,'x')+1,length(s)),'/')-1))
);

PS: If the formula fails, please provide the field value (displayed on the left hand side in the formula workshop) so that it is easier to debug rather than having to "figure out" what might have caused the problem.

Former Member
0 Kudos

try this

left ({USR_CR_BSA_ALERTS;1.A_DESC}, InStr({USR_CR_BSA_ALERTS;1.A_DESC}, "' "))

it should return what ever is to the left of what is in the ""