【问题标题】:How to extract substring from string in crystal report如何从水晶报表中的字符串中提取子字符串
【发布时间】:2020-11-04 16:39:19
【问题描述】:

我是水晶报表的新手。

我的数据(员工ID)是以下格式

Abc123, uttd333, ddt-435

我只想提取数字并删除前导字母和特殊字符。

还有一些值永远不应该被打印出来。

管理员 ID,例如

格力999,ttt999

我知道有一个 mid 函数,但这需要我指定子字符串应该开始的位置。这些值没有固定数量的前导字母。

有没有像我们在 SQL 中的 Ltrim 这样的东西可以用来在水晶报表中实现这一目标?

【问题讨论】:

  • 是否可以使用参数化 SQL 脚本或存储过程来处理数据库中的字符串操作?这将允许您使用不能在 Crystal Reports 中使用的正则表达式计算。

标签: database string crystal-reports substring


【解决方案1】:

Afaik CR 中没有内置函数可以从字符串中删除非数字字符,因此您必须自己动手(将 {Befehl.EmployeeId} 替换为您的字段):

StringVar employeeId := {Befehl.EmployeeId};
StringVar result := "";
NumberVar i;

// transfer numeric chars into result one by one
For i := 1 To Length(employeeId) Do
(
  If IsNumeric(employeeId[i]) Then
     result := result + employeeId[i];
);

// output result only if employeeId is not in your admin list
If employeeId In Split('Gree999,ttt999', ',') Then
  '--Admin--'
Else
  result;

【讨论】:

    【解决方案2】:

    下面是另一种仅获取字符串数字部分的方法: strReverse(ToText(Val(strReverse({EmpId})),0,""))

    它利用了知道数字总是在末尾的优势。

    【讨论】:

    • 嗨,这种方法使训练为零。例如,我的一个 id 被打印为 123 但实际上它是 12300 所以,它应该打印为 12300
    【解决方案3】:

    要处理尾随零,您可以使用:

    local stringvar withoutTrailingZeros := strReverse(ToText(Val(strReverse({EmpId})),0,""));
    withoutTrailingZeros + ReplicateString("0", Len({EmpId}) - (instr({EmpId}, withoutTrailingZeros) + Len(withoutTrailingZeros) - 1))
    

    但上面 mweber 的回答现在更简单了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多