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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crystal SQL and Date Parameters

Common Formulas

Crystal SQL and Date Parameters

by  HowardHammerman  Posted    (Edited  )
If you've run into a problem with a SQL database and Date (or DateTime) selection criteria, you're not alone. It seems that the parameter values want to save with the Crystal SQL, and it just doesn't work properly.

This is a known Crystal anomaly.

A way to work around it is to cast your Date (or DateTime) parameter and database fields as strings (in formula fields), manipulate them so that they're in the format YYYYMMDD, and do your selection on the formula fields. Do this and Crystal ignores them for the purposes of its SQL, which may solve your problem.

For example, say you have a Date-type parameter field called Date0. You can then create a formula called ParamDateStr like this:

StringVar STrDOB := ToText({?Date0});
NumberVar NumSlash1 := InSTr(StrDOB, "/");
NumberVar NumSlash2 := InStr(NumSlash1+ 1, STrDOB, "/");
StringVar StrYr := mid(StrDOB, NumSlash2 + 1, 4);
StringVar StrMo := left(STrDOB, NumSlash1 - 1);
StringVar StrDay := mid(STrDOB, NumSlash1 + 1, NumSlash2 - NumSlash1 - 1);
NumberVar NumMoLen := length(StrMo);
if NumMoLen < 2 then
StringVar StrMo := "0" + StrMo;
StringVar StrDay := mid(STrDOB, NumSlash1 + 1, NumSlash2 - NumSlash1 - 1);
if length(StrDay) = 1 then
StringVar StrDay := "0" + StrDay;
StrYr + StrMo + StrDay

What you've done is to parse the Date, then rearrange the year, month and day values (making sure that months and days are two characters in length). Now, for your database field:

StringVar STrDOB := ToText({Table.DateField});
NumberVar NumSlash1 := InSTr(StrDOB, "/");
NumberVar NumSlash2 := InStr(NumSlash1+ 1, STrDOB, "/");
StringVar StrYr := mid(StrDOB, NumSlash2 + 1, 4);
StringVar StrMo := left(STrDOB, NumSlash1 - 1);
NumberVar NumMoLen := length(StrMo);
if NumMoLen < 2 then
StringVar StrMo := "0" + StrMo;
StringVar StrDay := mid(STrDOB, NumSlash1 + 1, NumSlash2 - NumSlash1 - 1);
if length(StrDay) = 1 then
StringVar StrDay := "0" + StrDay;
StrYr + StrMo + StrDay

What you have NOW is two strings, each created from a date, and each formatted identically as YYYYMMDD. Finally, your selection criterion uses these two formula fields:

{@DOBString} >= {@ParamDateStr}

or whatever makes sense under your circumstances.


Howard Hammerman, Ph.D.
Hammerman Associates, Inc.
http://www.hammerman.com
800-783-2269
Hammerman Associates, Inc. provide Crystal Reports training,
consulting, course material, utilities and software. Consultants are available throughout North America for short or long-term assignments.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top