Possible Duplicate:
Conditional Check in Where clause
I have a few parameters that I can provide in order to search data from table:
- DateFrom
- DateTo
- Name
- TypeId
I have one code that I need to improve. In application code I have a function that get all this parameters and based on what parameters that I provide by using IF statements I build different queries.
I wonder is there better way to do this?
To make just one query with parameters and if there is no parameter ignore it.
So I don’t have to do this:
select...where DateFrom = 'someDate'
select...where DateFrom = 'someDate' and DateTo = 'someDate'
select...where DateFrom = 'someDate' and DateTo = 'someDate' and Name = 'someName'
etc.
Try the very simple approach, assuming your parameters have the names @Begin, @End, @Name:
Then if you pass the parameter’s value as NULL – then query will skip it