Range Data Types
The Range data type is a very useful data type that allows you to store multiple values within a single variable. It is used to store a range within one variable and perform tests to see if another variable falls within that range. However, you can’t just store any values in this variable. It must be a group of values with a definite starting and ending point. All values in between are included. Of course, it should go without saying that the starting point and end point must be of the same data type.
To declare a variable as a Range data type, declare it as one of the standard data types and put the Range keyword after the data type. The data types that are allowed to have a related Range data type are NumberVar, CurrencyVar, StringVar, DateTimeVar, DateVar, and TimeVar.
Scope datatype Range var;
Defining a range uses a variety of different operators. These operators specify the beginning and end of the range. The most basic operator is the To operator. It is placed between the start and end values. Using the To operator means that you want the start and end values to be included as valid members of the range.
//Demonstrate creating a range of all the days in a year.
DateVar Range DaysInYear;
DaysInYear := Date(#1/1/2002#) To Date(#12/31/2002#);
A variation of the To operator is to use it with the underscore character. Placing the underscore on one side of the To operator states that you want all values leading up to that constant to be included, but you don’t want the constant specified to be included. The underscore can be placed on either side, or both sides.
The following example demonstrates creating a range with all the days in a month. We only want to do so using the first day of the month in our range. The date 2/1/2002 won’t be included in our range. Instead, the last day of the month just prior to it will be included.
DateVar Range ExemptionPeriod;
ExemptionPeriod:= Date(#1/1/2002#) To_ Date(#2/1/2002#);
By placing the underscore on the right side of the To, then the end date won’t be included in the range. The range of valid dates for this formula is from 1/1/2002 thru (and including) 1/31/2002.
When you want to find out if a field or variable is included within a specified range, use the In operator.
If {Employee.HireDate} IN ExemptionPeriod Then
True
Else
False;
Crystal syntax has many predefined date range constants that can be used in your report. These are commonly used to filter out records that have dates that don’t fall within the specified range. Table 7-3 lists the predefined date ranges and specifies which dates are considered to be included. Many of these constants use today’s date to determine one of the end points of the range. You can tell the report to use a date other than the system date by setting the PrintDate property. This is done by selecting the menu options Report > Set Print Date and Time. Be warned that the PrintDate is saved with the report and will be the same even if you print the report in the future.
Table 7-4. Predefined Date Range Constants
Name | Description |
---|---|
AllDatesToToday | Includes all days prior to, and including today. |
AllDatesToYesterday | Includes all days prior to today. Today is excluded. |
AllDatesFromToday | Includes every day in the future, including today. |
AllDatesFromTomorrow | Includes every day in the future. Today is excluded. |
Aged0To30Days, Aged31To60Days, Aged61To90Days | Groups dates in 4 blocks of 30 days prior to today. Today’s date is included in the Aged0To30Days range. |
Calendar1stQtr, Calendar2ndQtr, Calendar3rdQtr, Calendar4thQtr | Groups dates in blocks of 3 months each. The first date is Jan 1 of the current year. |
Calendar1stHalf, Calendar2ndHalf | Start: Jan 1 of the current year thru June 30. |
End: July 1 of the current year thru December 31. | |
Last7Days | Start: The six days prior to today. |
End: Today. | |
Last4WeeksToSun | Start: The first Monday of the four weeks prior to last Sunday. |
End: Last Sunday. Note: Does not include the days after the last Sunday thru today. | |
LastFullWeek | Start: The Sunday of the last full week. |
End: The Saturday of the last full week. Note: Does not include the days after Saturday thru today. | |
LastFullMonth | Start: The first day of last month. |
End: The last day of last month. | |
MonthToDate | Start: The first day of this month. |
End: Today. | |
Next30Days, Next31To60Days, Next61To90Days, Next91To365Days | Groups dates in 4 blocks of 30 days after today. Today’s date is included in the Next30Days range. |
Over90Days | Includes all days that are older than 90 days from the report date. |
WeekToDateFromSun | Start: Last Sunday. |
End: Today. | |
YearToDate | Start: Jan 1 of the current year. |
End: Today. |