If statement for SELECT statement
Posted on 2012-04-03
What I would like to accomplish is to have a query pull data with 2 parameters if both parameters have a value. If only 1 parameter has a value, then only pull based off that parameter. Here is the original query:
SELECT col1, col2, col3, col4, col5 FROM table1 WHERE col3 LIKE '%' + @ID + '%' AND col5 LIKE '%' + @Param1 + '%'
I thought that the above statement would work no matter what and pull all values for @ID or @Param1 if the user did not enter a value for them. But it does not work. There has to be a value entered for both parameters for the query to work. So I was just wondering how I could use an IF statement to check and see if each parameter has a value and then pull absed on which parameters have a value