SQL Parameter Selector


This is something that i came across couple of times while working on a reporting project , and i hope it might help someone else 

We have a data set that we need to select form the database based on list of parameter , the client requirements was to have a selector that identify which filed to filter against with  start and finish date parameters 

table structure

UserID     ActionID    StartDate   FinishDate

the selection criteria was the user can select if they want to filter based on the start date or on the finish date , that means on the report there is only two date parameter

@startDate  and @EndDate

the solution was simple enough

  • I added a parameter called @filterselector of type boolean in the report 
  • in my query i added the following where 
((@filterselector = 1 OR 
 (dbo.I1_Storm_ProcessInstBuisnessArea.StartDate BETWEEN @StartDAte AND @FinishDate))
(@filterselector = 0 OR 
 (dbo.I1_Storm_ProcessInstBuisnessArea.FinishDate BETWEEN @StartDAte AND @FinishDate)))

simple and easy solution , please if you know a better way let me know :)