Blogs Home
Friday, April 10, 2020 12:00 PM

SSRS Tip: Using wildcards in Report parameters

Written by Thomas Harlan, Jim McGrath; Reporting Services Team - iatricSystems

Using-wildcards-in-Report-parameters-text-Free-text-delimited-lists-blog-header-March-2020Searching and filtering free text data can be a problem due to human error. Typos, transposed characters and even data entry into the wrong field present a challenge when trying to report on the data. In a SQL query we can use LIKE and wildcards to try to work around these issues.

By using LIKE in the query or stored procedure called by an SSRS report, we allow the report user to leverage SQL wildcards to improve the results of the report.

With the ability to arrange the display of parameters in the newer versions of SQL data tools, we can provide some hints for the user about using wildcards.

Our sample report captures surgical implants/explants from the Meditech Client Server scheduling tables. The rendered output of the report is not important as we are looking strictly at the parameters in use.

SQL Server T-SQL Wildcards

More information on using wildcards and the LIKE predicate can be found at LIKE (Transact-SQL).

SQL recognizes several wildcards that can be used in search predicates using the LIKE keyword.

% - matches all characters beginning at that position and/or bounded by the next character. ‘A%E’ will find:
  AE
  ABE
  ABCDE
  AAE
   
_ (underscore) replaces any single character at that position. ‘A_E’ will match:
  ABE
  ACE
  A1E
   
[ABC] will match either A, B or C at that position in the string. ‘A[ABC]E’ will match:
  AAE
  ABE
  ACE
   
[A-D] matches a range of characters at that position in the string. ‘A[A-D]E’ matches:
  AAE
  ABE
  ACE
  ADE
   
[^A-D] matches any character except the specified range. ‘A[^A-D]E’ matches:
  AEE
  AFE
  A1E

Sample report

Our sample report uses several free text parameters that can leverage wildcards. The parameters also allow the user to create custom lists using the semicolon (;) as a delimiter.

WC_Sample_Report

The four parameters on line 4 of the screen accept wildcards and delimited lists. Each parameter is also optional.

The parameters on lines 6 and 7 provide some hints for the user. We set the parameters to accept blank values with the text as the default value. While we cannot prevent the user from editing these parameters, the report simply ignores them.

We can create a list of catalog numbers with the wildcards described above to make our search more flexible:

WC_#1

The Lot Number, Serial Number, Batch Number parameters can be left blank or values can be entered to further refine the search.

The Stored Procedure

Inside our stored procedure we define the search parameters as VARCHAR(MAX)

WC_#2

Note that by allowing blanks in the report a value is passed in even if its empty (‘’).

First we create a temp table for each parameter:

WC_#3

Now populate each table using a split function:

WC_#4

Note You can append a % wildcard at the end of each string to automatically use the strings as a ‘beginning with’ function.

In the main query, we use LEFT JOIN and LIKE to capture matches:

WC_#5

Note that the Items in the wildcard lists are already forced into UPPER() case.

And then we do the final filtering in the WHERE clause with a CASE statement:

WC_#6

Note that by using this CASE statement we’re explicitly controlling the order of evaluation for the criteria in this section of the WHERE, this lets us check for no parameters being passed first, before do any more evaluation of the CASE statement.

Extra Credit

Consider if we can use COALESCE() instead of this case statement… it’s shorter to write, but does it return the same results?

WC_#7

And…

As ever, if you need help with Meditech DR index creation, reporting, extract or analysis please feel free to give your iatricSystems Account Executive a call or email info@iatric.com to discuss how we can help support your team!

RW-HELP-Button-Blue.png