We help IT Professionals succeed at work.

Crystal Reports, ver 8.5, extract portion of text field as number

LBarrett
LBarrett asked
on
1,584 Views
Last Modified: 2012-06-21
Hi,
I need to extract a portion of a text field (not memo) as a number.  Example:

Dust all rooms, 6 minutes per day, 5 times per week.
I want the 6 only

Dishwashing, 20 minutes per meal per day
I want the 20

Errand.  Pick up clients prescriptions from pharmacy. 15 minutes per week.
I want the 15 only

I will need to total these numbers as minutes.  The phrases can have more than one number, it can be preceded by a comma, period or whatever.  The only thing consistent is the word "minutes" in the field.  The field name is task.des.  Also not every field has minutes in it so I received an error unless I tested the field first like:

if {task.des} like "* minutes *"then...

and return blank or zero if there are no "minutes" if instr() is used.

Tried this from this forum:

stringvar sentence := {PwHcTelRptTasks.TskDes};
stringvar array words := split(sentence," ");
numbervar i;
stringvar result := "";

for i := 1 to ubound(words)
do
(
    if (left(words[i],1) = "(" and right(words[i],1) = ")") then
        (i := i + 1;
         "")
    else
        if isnumeric(words[i]) then
            result := words[i]
);
result;

it worked fine until the first example above of "6 minutes per day, 5 times per week" in this case the 5 was returned not the 6.

Any help would be appreciated!


Open in new window

Comment
Watch Question

Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
What do you want returned if MINUTES is not in the sentence?

I assume you want the number that is before the word minutes

Try the formula below

mlmcc

Local StringVar TestField := {task.des};
Local StringVar array Words;
Local NumberVar i := 0;
Local NumberVar index := -1;
 
If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) = 'minutes' then
            If IsNumeric(Words[i-1]) then
                index := i-1;
);
if index > -1 then
    Words[index]
else
    ' '

Open in new window

Author

Commented:
Thanks for the quick reply.  Returned the correct value but how to total this?  When I try to sum the fields I get the "this field must be numeric.." error.  Any suggestions?

Author

Commented:
If minutes is not in phrase then blank or zero doesn't matter as long as I can total the field.  And yes then value I need is the number of minutes.
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
If you need to total it then

if index > -1 then
    Val(Words[index])
else
    0

mlmcc

Author

Commented:
99% there. The formula works as does the totals, the one exception I have found so far is if the word "minutes" is followed by a period.  "15 minutes."  The formula returns zero.

Author

Commented:
Now that this is mostly working I was able to find another anomaly.  Often the word "minutes" is preceded by the word "extra":  "30 extra minutes" also the "number" of minutes can be the first word in the field such as: "20 extra minutes needed per day for...".  Also found this in a few instances: " 120 minutes for shopping and 30 extra minutes for accompanying."  I assume the formula would return the last value only.  If it could return both either as a total or even 2 separate values that would be great, if not then I can make do without it.

Since problem has increased in complexity I am raising the point value.
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
FOr multiple like  120 minutes for shopping and 30 extra minutes for accompanying."  
It should return the first one.  What do you need returned?

To handle the . you can use

If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) IN ['MINUTES','MINUTES.']  then
            If IsNumeric(Words[i-1]) then
                index := i-1;
);
if index > -1 then
    Words[index]
else
    ' '

COuld there be a , or other puncuation after the minutes?

mlmcc

Author

Commented:
I added 'MINUTES,' to the IN statement and found a few fields like this.   Still can't get the fields with just a single value and the word "extra" in front like  "add 30 extra minutes..." to return a value other than 0.  I am getting correct value for the minutes with the period now, thanks.  For the case of multiples like "120 for... and 30 extra minutes" ideal would be to return both values either separately or added together.
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
You can add EXTRA MINUTES to the list above and it should catch some of them if they are alone.  Too get both will reuire a different solution.  Let me think about it and I'll post my idea.

mlmcc

Author

Commented:
Added extra minutes like shown below: and it did not catch the minutes in the string "20 extra minutes needed for ....".  The field began with the "20" and did not include any other numbers.

Local StringVar TestField := {PwHcTelRptTasks.TskDes};
Local StringVar array Words;
Local NumberVar i := 0;
Local NumberVar index := -1;

if ({PwHcTelRptTasks.TskSts} like "Completed" )then
 
(If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) in  ['minutes','minutes.','minutes,','extra minutes'] then
            If IsNumeric(Words[i-1]) then
                index := i-1;
);

if index > -1 then
    Val(Words[index])
else
    0)

    else
    0
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
If you are going to UpperCase the text from the field the list should also be in uppercase.

To get all the values you will need to modify the loop


Local StringVar TestField := {task.des};
Local StringVar array Words;
Local NumberVar i := 0;
Local NumberVar index := -1;
Local NumberVar MinutesValue := 0;
 
If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) = 'minutes' then
            If IsNumeric(Words[i-1]) then
                MinutesValue := MinutesValue + Val(Words[i-1];
);
MinutesValue 
 
This will print 0 if the number isn't there
 
If you want it to print nothing then
 
If MinutesValue >0 then
   ToText(MinutesValue,0,'')
else
  ' '
 
mlmcc

Open in new window

Author

Commented:
Unsure about your reference above if I "UpperCase the text from the field..." and what that has to do with the formula retrieving the 20 from the string "20 extra minutes...".

Did change the formula as shown but still did not get any values returned when the value is followed by the word "extra".
Senior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Sorry for the delay was out of town.  This formula works great so the only outstanding part is the sentence with 2 values in it like " 120 minutes for shopping and 30 extra minutes for accompanying.." I was hoping to get both values returned separately or added together.  If this should be a new question that's fine.  I will award points for this question and open a new one later.

Thanks for the help.

Points to mlmcc

LBarrett

Author

Commented:
Thanks for the help. I am calling the solution complete since the question kept expanding beyond what I orginally asked.  Hopefully I will be able to get the rest of it figured out or ask a new question if needed.  Thank you again you were a great help.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.