简体   繁体   English

Crystal报告其他If语句

[英]Crystal Reports Else If statement

I can't figure out why this if statement won't work. 我不知道为什么这个if语句不起作用。 I have a DateTime field DATEFROM and a String parameter (it HAS to be String) periodEnd. 我有一个DateTime字段DATEFROM和一个String参数(必须为String)periodEnd。 I would like to calculate percentages depending if these two dates have 1, 2, 3 or more years difference. 我想根据这两个日期之间有1、2、3年或更长时间的差异来计算百分比。 When I use this formula I get either "100%" or "-" and never the other two options. 当我使用此公式时,将得到“ 100%”或“-”,而从没有其他两个选项。 It's like CR calculates the first IF and if it's true then: "100%" but if it's false, it never checks for the rest of the Else Ifs and goes dirreclty to Else 就像CR计算第一个IF一样,如果为true,则为:“ 100%”,但是如果为false,则它从不检查其余的Ifs,并直接处理Else

StringVar percentage:="";
If (cDate({?periodEnd})-{TABLE.DATEFROM})<=1 Then 
    percentage:="100%"
Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=2 Then 
    percentage:="66%"
Else If (cDate({?periodEnd})-{TABLE.DATEFROM})<=3 Then 
    percentage:="33%"
Else 
    percentage:="-"

Any idea? 任何想法?

a) I assume you already made sure your periodend format matches with what cdate interprets? a)我假设您已经确定自己的periodend格式与cdate解释的格式匹配?
If you just subtract them, Crystal by default returns the number of days between. 如果仅减去它们,则默认情况下,Crystal会返回之间的天数。
Two ways you can do year: 一年可以做的两种方法:

datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM})

or 要么

year(cDate({?periodEnd})-year({TABLE.DATEFROM})

b) You've also no doubt accounted for when your {TABLE.DATEFROM} is greater than cDate({?periodEnd} and you have a negative result? b)当您的{TABLE.DATEFROM}大于cDate({?periodEnd}且结果为负数时,您也毫无疑问地得到了解释。
Not sure if the following is the behavior you would want, but I'm throwing it in for your information 不确定以下是否是您想要的行为,但我将其提供给您参考

ABS(datediff("yyyy",cDate({?periodEnd},{TABLE.DATEFROM}))

**make sure you check the help file for the datediff codes ("yyyy" etc) as they are not quite instinctive and can trip you up when you think you're using the obvious one **请确保您检查了帮助文件中的datediff代码(例如“ yyyy”等),因为它们不是本能的,当您认为自己使用的是明显的代码时,可能会使您绊倒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM