Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Syntax Question Post Reply Post New Topic
Page  of 2 Next >>
Author Message
Mojoflow1000
Newbie
Newbie
Avatar

Joined: 17 Feb 2014
Location: United States
Online Status: Offline
Posts: 7
Quote Mojoflow1000 Replybullet Topic: Syntax Question
     Posted: 18 Feb 2014 at 5:26am

I am developing a Crystal report and I need to identify records where a single-medication-dosage was dispensed 5 or more times in one day. 

There are many issues related to accomplishing this but the one I wanted to ask you about is this:
 
The field that contains the "single medication dosage" is a VARCHAR.
 
- This field can contain values like 120 MG, 3.5 MG, etc.
 
The field that indicates the amount dispensed is also a VARCHAR and it can contain values like: 40 MG, etc.
 
The discrete amount dispensed will not always equal the single medication dosage.  Maybe the single medication dosage is 120 MG... well, the amount dispensed each time in one day, may only be 40 MG. 
 
* What I was hoping to do was:
 
1) Convert these fields to a number but I don't see how this is possible since it would require stripping the free text portion, "MG" for example, and (since there can be decimals in the actual number itself) you don't want to strip the decimal.
 
This is my question.  How can I work with these VARCHAR fields as numbers and remove the MG free text but maintain the decimals?
 
 
BACKGROUND INFO IF NEEDED:
 
If I was able to convert to a number, then I wanted to sum the dispenses per day, divide by 5 and compare the result to the single dosage strength.  If equal to or greater, then the record should be displayed.   
 
Example:
 
Strength: 120 mg
 
Dispensed discrete dosage for Feb 20, 2014
 
9am: 40 MG
12pm: 40 MG
3pm: 120 MG
6pm: 40 MG
 
The result: For this day, a total of 240 MG was dispensed.  240 divided by 5 = 48 (which is less than the strength of 120) so we know there were not 5 or more discrete dosages dispensed.  This record would not be displayed in the report. 
 
Thank you
- Mike 
 
 
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 18 Feb 2014 at 10:30am
if the string always has the "MG" in it, then you could strip off the "MG" and convert the rest to a number (i.e., val(left({dosage},len({dosage})-3))  ).
IP IP Logged
Mojoflow1000
Newbie
Newbie
Avatar

Joined: 17 Feb 2014
Location: United States
Online Status: Offline
Posts: 7
Quote Mojoflow1000 Replybullet Posted: 18 Feb 2014 at 10:39am
Unfortunately, the values can contain all kinds of different characters in addition to the numbers, like: %, U, GM, U/GM, ML, MCG/ACT.  I appreciate the help though.  I'm thinking this just might not be possible with the fields being varchar and containing all the different characters.
IP IP Logged
Mojoflow1000
Newbie
Newbie
Avatar

Joined: 17 Feb 2014
Location: United States
Online Status: Offline
Posts: 7
Quote Mojoflow1000 Replybullet Posted: 18 Feb 2014 at 11:30am
I found some formula syntax (see it at the very bottom) that appears to be on the right track.  It will look at a string and check to see if the *last* character is numeric.  If it is, it will strip that character.  If it is not numeric, then the syntax will leave the string unaltered. 
 
I thought that if I posted this syntax, maybe someone could determine how to modify it to check all characters that are non numeric within a string (except a decimal).  I've tried and failed unfortunately. 
 
So, what I need it to do is remove all non-numeric characters (to the right of the last number) instead of just the last non-numeric character):
 
Values could be like below .......... and I need them to appear like:
 
2 MG .......................... 2
3.25 U ....................... 3.25
1 % ........................... 1
25.00 MCG/ML ............ 25.00
.2 ................................ .2
5 MU/VIAL ................... 5
100 %......................... 100
etc...
 
Note: decimals are maintained.
 
I would like to remove all characters starting on the right and move back to the first number.
 
Here is the syntax that checks the last character in a string and removes it if non-numeric:
---------
 
if NumericText(mid(string,length(string)-1,1))
// is the last char numeric
then
      string                                                       
// keep the whole string
else
      mid(string,1,length(string)-1)                  
// drop the last character
 
 ----
Thank you
 
 
 
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 18 Feb 2014 at 11:30am
You might be able to do it character by character.  I will see if I can work up a formula that might work.
IP IP Logged
Mojoflow1000
Newbie
Newbie
Avatar

Joined: 17 Feb 2014
Location: United States
Online Status: Offline
Posts: 7
Quote Mojoflow1000 Replybullet Posted: 18 Feb 2014 at 11:31am
Awesome!
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 18 Feb 2014 at 11:45am
Here is the beginning of a formula.  I am not sure if you will need to check for %.  Of course you then will need to convert the string to a number.

local stringvar finalnum;
local numbervar i;
for i := 1 to len({dosage}) Do (
if mid({dosage},i,1) = "." then finalnum := finalnum+ "." else
 if isnumeric(mid({dosage},i,1)) then finalnum := finalnum+mid({dosage},i,1)
);
finalnum;


Edited by kevlray - 18 Feb 2014 at 11:45am
IP IP Logged
Gurbs
Senior Member
Senior Member
Avatar

Joined: 16 Feb 2012
Location: Ireland
Online Status: Offline
Posts: 216
Quote Gurbs Replybullet Posted: 18 Feb 2014 at 11:03pm
isn't the option split a possible solution? split your field based on spaces, and use the first "field"?

something like
tonumber(split({table.field}," ")[1])

This will only work if there is a space between your free text and your numbers though
IP IP Logged
Mojoflow1000
Newbie
Newbie
Avatar

Joined: 17 Feb 2014
Location: United States
Online Status: Offline
Posts: 7
Quote Mojoflow1000 Replybullet Posted: 19 Feb 2014 at 4:30am
Thanks for your help, I really appreciate it.  I don't think there is any need to continue with this because I've learned the source data is so varied it does not seem possible to accommodate all possibilities.
 
kevlray, your code is doing exactly what I requested and stripping everything except the numbers.  Thanks!  But the values are all over the road and too varied to manipulate all possibilities properly. 
 
All kinds of variations exist in the field we're trying to manipulate, things like:
 
(2.5 MG/3ML) 0.083%  which turns into 2.50.083
100 mg/100 ml turns into 100100 
 
It's as if manual investigation of the value is required to determine which portion (of the numbers) represents the actual dosage in some cases.
 
Gurbs, the syntax checker returns no errors fro your code in the formula editor and, when I run the report, it seems to be doing what is required for a portion of the values.  But, as I advance through the pages of the report, I will get an error that says "the string is non-numeric" and it opens the formula in the editor.  It appears to be failing when it encounters this string value: (2.5 MG/3ML) 0.083%
 
Anyway, thanks for giving it a go guys, I appreciate it!
 
 
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 19 Feb 2014 at 6:17am
With that data, I think it would be impossible to get anything to work correctly.
IP IP Logged
Page  of 2 Next >>
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum