### Dynamically filtering queries

You can use parameter in a WHERE clause so that the query result can vary each time according to the entered parameter value(s). See the following example.

1. Open the demo catalog SampleReports.cat, then in the Catalog Browser, expand the desired data source and create a type-in parameter named IDSet of String type with the default value 1,10,20 (leave the other settings to their default).
2. In the same data source, create a query CustomersInfo on the table Customers. Select all the fields contained in the table.
3. Filter the records of the query by adding a condition as below in the Search Condition dialog (for details, see Filtering with the filter format):

4. Create a table report based on the query as follows: have the fields Customer ID, Customer Name, City and Phone displayed in the table and apply the default style.
5. View the result with the default value of IDSet. You will find that the records with Customer ID equal to 1, 10 and 20 have been retrieved. See the result.

6. View the report again. This time, enter the IDSet value as 3,7,9,11,15,25 (no space). Now, the records for the specified Customer IDs are displayed. See the result.

Notes:

- The parameter type should be set according to the field type and the manner in which you use the parameter. For example, if the field is a number, define a parameter to be Number type also.
- If you set the parameter format to be @ParameterName, JReport Engine will parse the parameter value. That is, if you input a parameter value with single quotation marks, the quotation marks will be recognized as parts of the parameter value. However, in the format :ParameterName, JReport Engine will not parse the parameter value, but instead will replace the original value with the parameter value input at runtime. That is, if ':' is used and the parameter type is a string, the system will simply substitute <ParameterName> with the parameter value string. In this case, you need to key in a parameter value like:

  'asdfsakdf','aksdfashdlf','asdfjkasdfalk'...

  If the data type is a number, the parameter value should be like:

  12,34,56,...

Now, what if you do not want to enter a parameter value when viewing the report result, so that all records will be fetched out? To achieve this goal, you can use a formula along with the parameter.

Take the following steps with the report created above:

1. Create a parameter Parameter1 of String type, and leave its default value empty.
2. Create a formula Formula1 as below:

       string s = Trim(@Parameter1);
       integer len = Length(s);
       string cos = Trim(@"State");
       string coL = "," + cos;
       string co = coL + "," ;
       string coF = cos + "," ;
       integer lenF = Length(coF);
       integer lenL = Length(coL);

       if (len <1)
       return true
       else if (s == cos)
       return true
       else if (InStr(co, s) >= 0)
       return true
       else if (Right(s,lenL) == coL)
       return true
       else if (Left(s,lenF) == coF)
       return true
       else
       return false

   Where, Parameter1 is the just created parameter, and State is the DBField of the query CustomersInfo with which you want to filter the records.

3. Modify the filter condition of the query set in the above example by deleting the existing row and making a new one: Formula1=true.
4. View the result. You will be prompted to enter parameter value. Enter NY, then the record with State equal to NY will be fetched out. Enter NY,MN,CA,TX,CO, then the records with State equal to these states will be fetched out. Then, leave the parameter value empty, and all records will be displayed.