I have a string field that mostly contains numeric decimal values, but sometimes contains values like "<0.10" or "HEMOLYSIS".
I want to use a formula to convert these numeric value strings to values, leaving non-values blank (null).
if isNumeric({a_omgang.omg_resultat}) then
toNumber({a_omgang.omg_resultat})
returns 0 for all non-numeric values, which makes it hard to calculate e.g. average or mean, or to count the number of values. (The latter can of course be achieved by using a running total count with isNumeric
evaluation formula.)
Any suggestions how I can get the formula to work as I want?
Edit: I want the value to be blank (null), not just turn of visibility if non-numeric.
In the past, I've created a SQL Expression that returns a NULL:
-- {@DB_NULL}
-- Oracle syntax
(
SELECT NULL FROM DUAL
)
-- {@DB_NULL}
-- MS SQL syntax
(
SELECT NULL
)
Then I reference this field in the formula:
// {@FormulaField}
If IsNumberic({table.field} Then
ToNumber({table.field})
Else
ToNumber({@DB_NULL})
You can get this to work by doing the following:
- Create a new formula and just enter a number into it, then save. Crystal will now associate this formula with a numeric return value.
- Go back into the formula and delete the number and resave it. Now you have a formula that returns a null, but CR has already associated it as being a numeric formula so you can now use it anywhere you could use a numeric type.
if isNumeric({a_omgang.omg_resultat})
then toNumber({a_omgang.omg_resultat})
else {@NullNumeric}
Note that you can also use this for any other data types (including strings since an empty string is not equivalent to a null string) and it is extremely useful for using with summary functions where you just want to straight-up ignore certain rows.
Not sure if this is the best solution, but it is a solution.
Create a running total formula for the maximum of the field. Evaluate by formulaNumericText(Replace({a_omgang.omg_resultat}, ".", ","))
and reset by formulatrue
This will give you the maximum result of every entry. (I.e. that entry.) Only entries that can be converted to a value will be evaluated, so all others will be null. Having true
as resetting formula makes sure that you always get the value of the entry being evaluated.
The result of this running total can then be converted to a value using the formula in the question.
The Replace()
is due to locale and may not be needed depending on your system settings. If needed, it will also have to be used in the value converting formula.
To the best of my knowledge you can't do this directly. If a numeric formula field returns a null it is converted to 0.
You say you can't suppress. Could you convert to a number then back to a string?
来源:https://stackoverflow.com/questions/13858187/have-a-crystal-reports-formula-convert-numeric-strings-to-values-but-leave-non