Click here to Skip to main content
15,074,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the following query to return records according to @date and @abc passed.If i did not pass any value for param @abc then query should just consider the date conndition and return records according to that.what changes to do in following query?
at present i have the following query..
select * from table where date=@date and abc=@abc
Posted

You can use if...else... condition to check whether it is having data or not.

if @date and @abc is not null
begin
    "Your select statement" Where date=@date and abc=@abc
end
if @date is not null and @abc is null
begin
    "Your select statement" Where date=@date 
end
if @date is null and @abc is not null
begin
    "Your select statement" Where abc=@abc
end
   
Comments
CHill60 4-Dec-15 3:55am
   
Question was answered correctly (and answer accepted) nearly 2 years ago.
This solution is not just very poor but does not actually answer the original question as asked
Bhavikkumar Chaudhari 4-Dec-15 6:50am
   
Thanks...!!! :)

That make sense.
You should check such a a condition in your code and execute a different query, namely
SQL
select * from table where date=@date

for the purpose of ignoring the 'empty' abc parameter.
   
Try:
SQL
SELECT * FROM table WHERE date=@date AND (@abc IS NULL OR abc=@abc)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900