I’ve got an ASP.net search page where the user can enter one or more search criteria. The page calls a stored procedure to query a MS SQL Server 2008 db.
Part of the search criteria is single date or date range. If the user supplies Date1, we search on a single date. If the user supplies Date1 and Date2, we search on a date range.
My issue is coding this logic in the stored proc.
@Date1 datetime
@Date2 datetime
..other search params...
So there are three conditions:
- Both @Date1 and @Date2 are null (the user is not searching on dates)
- @Date1 is not null and @Date2 is null (the user is searching on a single date)
- @Date1 is not null and @Date2 is not null (user is searching a date range)
I can’t figure out how to structure the WHERE clause to handle each of the three possible conditions.
I’m familiar with ISNULL() and COALESCE()
Any tips or suggestions are greatly appreciated.
Another choice, losing some expressiveness but likely using indexes, could be: