Print Page | Close Window

Comparing dates in formula

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=1016
Printed Date: 01 May 2024 at 6:45am


Topic: Comparing dates in formula
Posted By: tlmoses
Subject: Comparing dates in formula
Date Posted: 17 Jul 2007 at 10:33am
I would like to be able to compare dates to know which was later. 
I have Date1 and Date2, where sometimes Date 2 is not set.
 
If I do this:
 
if Date2>Date1 then
   Date2                            
else
   Date1
 
I always get Date 2, even when there isn' t a date set in the field.  I would like to get Date2 if it is later and if it is not set, use Date1.  These values are declared as dates in the table that I pull them from .
 
How do I compare them?



Replies:
Posted By: jkwrpc
Date Posted: 17 Jul 2007 at 2:13pm

You might try this.

Test Date2 to make sure it is a vaild date by using the ISDate function. Your formula would look something like this.

------------------------------

datevar x; 
 
IF IsDate({Date2}) AND Date2>Date1 THEN
  x:= Date2                            
else
   x := Date1;
x;
-----------------------------------

Hope this helps
 
 
John W.
http://www.CustomReportWriters.net - www.CustomReportWriters.net


Posted By: tlmoses
Date Posted: 18 Jul 2007 at 5:14am
Thanks for the suggestion, but I have tried "isdate" and I get a Crystal error that it is expecting a string.


Posted By: jkwrpc
Date Posted: 18 Jul 2007 at 7:51am
I tried this much of the formula with the changes and this much works.
--------------------
stringvar x;
 
IF IsDate(Cstr({Orders.Ship Date}))  THEN
  x:= (Cstr({Orders.Ship Date}))                   
else
   x := "Nodate";
x;
----------------------
You will need to deal with the 'AND Date2>Date1' issue, I did not have two dates in my sample database to quickly compare.  You could also use the totext function of CR instead of the CStr
 
Hope this helps
 
Regards,
 
John W.
http://www.CustomReportWriters.net - www.CustomReportWriters.net


Posted By: tlmoses
Date Posted: 18 Jul 2007 at 9:57am
Thanks - I solved my problem with the last suggestion!



Print Page | Close Window