Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I am having a problem in writing a select query .

I have written a stored procedure which takes two arguements input1 and input2,
where input2 can be null.In my stored procedure I am writing a select querry (which uses joins )and where condition which should ullfill below requirement.

1.when input1 and input2 are not null ,give the result (column1=input1 and column2=input2) (Result should have both vaues matched)

2.Input1 is matched and input2 is null-Return results for matched for input1

3.Input1 is not matched input2 is not null,Return matched for input2

Please help me.
Posted

try and use the where clause with

isnull(Column1,0) = 0

I have not tried on query, it may work.
 
Share this answer
 
Comments
Prashant Bangaluru 13-Dec-12 1:18am    
Thanks for your reply,,But its not working for me This is the code snippet I am using .. INNER JOIN @TEMPFLOORS j ON j.BuildingId = g.BuildingId WHERE @index='9' and ((g.propertynumber=@input1 and (j.AllFloors=isnull(@input2,j.AllFloors )))) and (f.scheduleddate>=@From and f.scheduleddate<=@To) Please help me where I am missing
Try
SQL
SELECT * FROM yourTable
WHERE Column1 = ISNULL(@input1, Column1) AND Column2 = ISNULL(@input2, Column2) 
 
Share this answer
 
Comments
Prashant Bangaluru 13-Dec-12 1:18am    
Thanks for your reply,,But its not working for me
This is the code snippet I am using ..
INNER JOIN @TEMPFLOORS j ON j.BuildingId = g.BuildingId
WHERE
@index='9' and ((g.propertynumber=@input1 and (j.AllFloors=isnull(@input2,j.AllFloors ))))
and (f.scheduleddate>=@From and f.scheduleddate<=@To)

Please help me where I am missing
__TR__ 13-Dec-12 1:40am    
Looks like you have missed to add ISNULL for @input1.
g.propertynumber=ISNULL(@input1, g.propertynumber)
Prashant Bangaluru 13-Dec-12 1:44am    
Ya Thanks a lot..I am getting result but only missing 3rd condition,If the floor name is present but input1 is not matching it should give result for rows for matching floorname
__TR__ 13-Dec-12 2:34am    
I don't see a floorname column in your where clause.
Prashant Bangaluru 13-Dec-12 3:46am    
Oh I am very very sorry It is actually J.allFloors ..
SQL
declare @val1 varchar(10) ;
declare @val2 varchar(10) ;

set @val1='s';
set @val2='ur';


EASIEST WAY TO GET THE RESULT IS :-

select * from table_Name  where col1=isnull(@val1,col1) and col2=isnull(@val2,col2)

or
use the dynamic SQl code like

declare @val1 varchar(10) ;
declare @val2 varchar(10) ;
declare @dynaimcstring varchar(200);
declare @executestring nvarchar(max);
set @val1 ='s';
set @val2='';

if( ltrim(rtrim(@val1))='' and ltrim(rtrim(@val2))!='')
begin
set @dynaimcstring='where col2='''+@val2+'''';
end
if( ltrim(rtrim(@val1))!='' and ltrim(rtrim(@val2))='')
begin
set @dynaimcstring='where col1='''+@val1+'''';
end
if( ltrim(rtrim(@val1))!='' and ltrim(rtrim(@val2))!='')
begin
set @dynaimcstring=' where col1='''+@val1+''' and col2='''+@val2+'''';
end
if( ltrim(rtrim(@val1))='' and ltrim(rtrim(@val2))='')
begin
set @dynaimcstring='';
end
set @executestring='select * from allot_Det '+ @dynaimcstring;
print @str;
exec sp_executesql @str;
 
Share this answer
 
Comments
Prashant Bangaluru 13-Dec-12 1:46am    
Thanks for your solution..But I am using a stored procedure which is having 10 unions so I have to do everything in where condition only

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