Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Subtract CurrentDate year based on IF statement

Status
Not open for further replies.

vbit

Programmer
Jul 31, 2001
47
US
Usuing CR 7 with SQL database.

I am trying to create a formula that displays the previous year if certain conditions are true.
For example:

X is number field

if X > month(CurrentDate) then ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"] [X] + " " + ToText((CurrentDate -1),"yyyy")
else
["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"] [X] + " " + ToText((CurrentDate),"yyyy")

The problem is that even though X is greater than month(CurrentDate) it still does not give me last year. It always shows the current year.

Thanks.
 
Add parenthesis around your "do this stuff" of your If Then Else. The are in bold below.

if X > month(CurrentDate) then (["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"] [X] + " " + ToText((CurrentDate -1),"yyyy"))
else
(["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"] [X] + " " + ToText((CurrentDate),"yyyy")"))

Mike

 
My first one was wrong. I had altered the formula differently than I posted. Sorry.
The following actually works.

if X> month(CurrentDate) then (["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"] [X] + " " + ToText(year(CurrentDate) -1,0,""))
else
(["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"] [X]+ " " + ToText(year(CurrentDate),0,"") ) Mike

 
Just what I needed. Thank you.
 
Here is another way to do this.

You can get the dateadd function from Crystal.

if x >month(CurrentDate)then dateadd("yyyy", -1, CurrentDate)
else date(year(CurrentDate), x, 01)

This will subtract the year correctly and use the previous year or use the current year when needed.

alley
 
I didn't think that DateAdd was available for Crystal 7.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top