Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help extracting part of string Crystal XI 1

Status
Not open for further replies.

jpeters01

Technical User
Dec 5, 2007
109
US
I am trying to extract data from a string...my data looks like this:

Strength: 4 mg
Strength: 30 ml before bed
Special instructions: On full stomach
Special instructions: Take before breakfast Strength: 20 mg
and sometimes the field is null/empty

I need to pull the text from the strings starting with the word "Strength" like this:
Strength: 4 mg
Strength: 30 ml before bed
Strength: 20 mg

Can you help me with the formula please?




 
Here is part of a solution:

Code:
whileprintingrecords;
stringvar x1 := instr({table.field},"Strength",1);
stringvar x2 := instr(x1+1,{table.field},"Strength",1);
stringvar x3 := instr(x2+1,{table.field},"Strength",1);
stringvar x4 := instr(x3+1,{table.field},"Strength",1);
stringvar output := ""
output := output + {table.field}[x1 to x2-1];
output := output + {table.field}[x2 to x3-1];
output := output + {table.field}[x3 to x4-1];
output := output + {table.field}[x4 to len(trim({table.field}))];
output

As you can see I have allowed for only four instances of the word "strength". You can add more.
And this is crying for the use of a do loop rather than the stripped down code I have provided.
Also you will get more text than you want. But this is a start.

Howard Hammerman,
Crystal Training and Crystal Material
On-site classes and one-on-one coaching
Low-cost telephone/email support
 
There were errors in the code in my reply above. Here is better code:
Code:
whileprintingrecords;
numbervar x1 := instr({table.field},"Strength",1);
numbervar x2 := instr(x1+1,{table.field},"Strength",1);
numbervar x3 := instr(x2+1,{table.field},"Strength",1);
numbervar x4 := instr(x3+1,{table.field},"Strength",1);
stringvar output := "";
output := output + {table.field}[x1 to x2-1];
output := output + {table.field}[x2 to x3-1];
output := output + {table.field}[x3 to x4-1];
output

Howard Hammerman,
Crystal Training and Crystal Material
On-site classes and one-on-one coaching
Low-cost telephone/email support
 
It isn't clear to me whether the sample data is a single row (comprising 4 lines), or 4 rows of data. Assuming the latter:

Code:
If      Instr({Table.Field}, 'Strength') > 0
Then    Mid({Table.Field}, Instr({Table.Field}, 'Strength'))

Cheers
Pete
 
Thanks for your reply Howard, however Pete's suggestion looked simpler and it is working beautifually for me. Thank you Pete!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top