Have a Crystal Reports formula convert numeric strings to values, but leave non-numeric blank/null

喜夏-厌秋 提交于 2019-12-05 10:10:35
craig

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:

  1. Create a new formula and just enter a number into it, then save. Crystal will now associate this formula with a numeric return value.
  2. 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 formula
NumericText(Replace({a_omgang.omg_resultat}, ".", ","))
and reset by formula
true

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!