Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a stored Procedure with 2 input parameters. one is Type and other is Institute.

Institue may be null sometime. So if the institute is null it should not be in where condition.

For example I should have query like below. Can I do with single query? Not using IF.

SQL
IF @type =1  and @Inst = 'abc'

Select * from Test
where Type = @type and Institute = @Inst 

If @type =1  and @Inst = null

Select * from Test
where Type = @type 


I need the above example with one select with out using IF coniditon. IS that possible?

Thanks for your help.
Posted
Updated 20-May-18 23:49pm
v2
Comments
Richard C Bishop 5-Mar-13 10:14am    
You can use case statements.

SQL
Select * from Test
where Type = @type and (Institute = @Inst OR @Intst IS NULL)
 
Share this answer
 
Comments
vasini 5-Mar-13 10:31am    
Thanks for the answer. But it includes records when inst is NULL. I need includes records when its passed else just condition with type.
ZurdoDev 5-Mar-13 11:03am    
I don't quite understand what your saying, but I think it is this: (Institute = @Inst OR (@Intst IS NULL AND @type = 1))
SQL
select * from Test
where Type = @type and 
(coalesce(@inst, 1) = 1  or Institute = @inst)


coalesce returns first of its argument which is not null, so when @inst is null your condition will be like Type = @type and (1=1 or Institute = null).
The case when @inst has value will be Type = @type and ('abc' = 1 or Institute = 'abc').
I hope this helps.
 
Share this answer
 
v2
Hi,

Check the following links...

you will get Idea about NULL

Handling Null Values [^]
Handling NULL values in SQL Server 2005[^]
NULL, NULL, NULL and nothing but NULL[^]
NULL Functions[^]
A Puzzle – Fun with NULL[^]

Regards,
GVPrabu
 
Share this answer
 
v3
SQL
Select * from Test
where Type = @type and (@Inst is null or Institute=@Inst)
 
Share this answer
 
v2
Comments
D.Nikolov 9-Mar-13 18:42pm    
This is simpler than mine solution. I would use this when possible.
Try this.

SQL
DECLARE @UserName nvarchar(40), @Password nvarchar(10)

set @UserName='admin';
set @Password=null;

declare @sql nvarchar(4000);

set @sql='Select * from AdminUser where (';

if( @UserName is not NULL)
BEGIN
set @sql=@sql + ' UserName = '''+@UserName +'''';
END

if( @Password is not NULL)
BEGIN
if( @Password is not NULL)
set @sql=@sql + ' and Password = '''+@Password +'''';
END

set @sql=@sql + ')';

print @sql
exec sp_executesql @sql


Happy Coding :)
 
Share this answer
 
Select * from Test
WHERE
Type = ISNULL(@type,type)
AND Institute = ISNULL(@Inst,Institute)
 
Share this answer
 
v2
Comments
jaket-cp 20-Oct-15 3:47am    
This question is more than 2 years old, not a good idea to answer old questions.
Also this question has an accepted solution.
On top of that, it does not appear as though your solution will get what the op asked for.
decalre @ins varchar(50)

if @ins = null
begin
@ins = ''
end


Select * from Test
where Type = @type and Institute like '%' + @ins + '%'

I hope it will help you !
 
Share this answer
 

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