Search code examples
reporting-servicesreportingservices-2005

Making a filter parameter optional


I am using SSRS 2005. I created a report parameter named icVar. What should i do to allow this filter to be optional. Meaning if user enter something into the textbox, will display result according to what is entered but if nothing is entered it will just treat it to display all. This is the query in my dataset.

SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, COMPANY_M,  
CONSECUTIVE_D
FROM TEMP_TARGET
WHERE (CONSECUTIVE_D >= @consecDays) AND (ENTRY_DT BETWEEN @startDate AND @endDate) AND
(ACCESS_LOCATION_X LIKE @accessVar) AND
(IC_N LIKE @icVAr) 

I am making use of the wildcard % for my icVar. So if user input S123 for that filter the report will display result where IC_N values starting with S123... However when nothing is entered for that filter, do not force the user to input anything. It shall show IC_N Like %(means all) if nothing is entered.

Any help is appreciated. Thanks.


Solution

  • Just use expression evaluation short-circuiting - test for Null first and only if it is not Null does the next part of the expression evaluate, testing for LIKE (see the condition on the last line):

    SELECT PASS_M, ENTRY_DT, EXIT_DT, WORKED_HRS, ACCESS_LOCATION_X, IC_N, COMPANY_M, CONSECUTIVE_D
    FROM TEMP_TARGET
    WHERE (CONSECUTIVE_D >= @consecDays) 
    AND (ENTRY_DT BETWEEN @startDate AND @endDate) 
    AND (ACCESS_LOCATION_X LIKE @accessVar) 
    AND ((@icVar IS NULL) OR (IC_N LIKE @icVAr))