Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: IF function in selection criteria Post Reply Post New Topic
Author Message
martinbanks
Newbie
Newbie


Joined: 09 May 2012
Online Status: Offline
Posts: 18
Quote martinbanks Replybullet Topic: IF function in selection criteria
     Posted: 11 Jun 2012 at 8:04am
Am i able to use and IF statement in my selection criteria?

What i am trying to achieve is for CurrentDate to be -1 for all days except Monday, when it will be -3. This is what I put but it doesn't seem to like it:

if DayOfWeek = "2" Then {ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-3
Else {ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-1
IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 11 Jun 2012 at 8:07am
You need a field for the function Dayofweek (i.e., DateOfWeek(CurrentDate)) and it is equal to a number not a string.  I think the rest of the If statement is correct.
IP IP Logged
martinbanks
Newbie
Newbie


Joined: 09 May 2012
Online Status: Offline
Posts: 18
Quote martinbanks Replybullet Posted: 11 Jun 2012 at 8:12am
Fantastic, thank you very much.

I changed it to if DayOfWeek(CurrentDate) = 2 Then {ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-3
Else {ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-1 and it appears to work. Today it is looking at Friday. I guess I'll see if it works tomorrow if it brings up today's figures :)
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 11 Jun 2012 at 11:09am
as an add on I would recommend not using if then in your select statement. Instead I would use 'and' and 'or' like this...
(DayOfWeek(CurrentDate) = 2 and{ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-3 )
or
(DayOfWeek(CurrentDate) <> 2 and {ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-1)
IP IP Logged
martinbanks
Newbie
Newbie


Joined: 09 May 2012
Online Status: Offline
Posts: 18
Quote martinbanks Replybullet Posted: 11 Jun 2012 at 11:47am
Thanks for the tip. Does that run faster?

I also have other criteria so will i need to wrap that whole thing in parentheses?

ie ((DayOfWeek(CurrentDate) = 2 and{ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-3 )
or
(DayOfWeek(CurrentDate) <> 2 and {ARENTITYPRVA_ARENTITY.ZZENCONTDATE} = CurrentDate-1))
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 12 Jun 2012 at 4:34am
If you have other conditions then you are correct on the parenth wrapping.
The select formula just wants a boolean evaluation result from each condition per row.  
I am not 100% sure that it is more efficient processing but I think that it is. I have not found an instance where an if-then used in the select statement could not be easily worked in to an OR statement.
If you break it down these are the 2 statements
if-then is
IF T/F THEN t/F ELSE T/F
 
the OR statement is
(T/F and T/F)
or
(T/F and T/F)
 
The if-then does not set the date value it is still just returning a true/false
 
I also need to give a shout out to Hilfy for helping me understand this (and aplogize to Hilfy if I have not explained this correctly Embarrassed).


Edited by DBlank - 12 Jun 2012 at 4:44am
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum