Click here to Skip to main content
15,885,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
How I use operator in case statement,please see bellow example.

SQL
CreatedByClient =
case @AddedBy
when 0 then  CreatedByClient
when 1 then 0
when 2 then >0
end

I write a case based on @AddedBy ,I am try to when @AddedBy value is 2 then CreatedByClient>0 how do I do this.please help.

Update

Hi ,thanks to all for reply,please see bellow

SQL
987
458
0
45
0
0
0
58
569
2


for query I pass 0,1,2,for 0 return all,for 1 return only 0 and for 2 return only non 0 rows.

please assists me how do I do this .
Posted
Updated 4-Jan-15 22:40pm
v2
Comments
Jitendra Ku. Sahoo 5-Jan-15 3:51am    
You can have a look here http://msdn.microsoft.com/en-IN/library/ms181765.aspx
KuntalBose 5-Jan-15 3:55am    
Hi,thanks
I see the link,but not see a example how to set operator in then for my situation
when 2 then >0
CHill60 5-Jan-15 3:58am    
You can't set a value to '>0' try setting it to 1 instead. What are you actually trying to achieve?

If I understand your criteria correctly, depending on what the value of @AddedBy is you want different record/results.
You could try something like this:
SQL
--setup test data
declare @TabNum table(id int identity(1,1), num int);
insert into @TabNum
	select 987 num
	union all select 458
	union all select 0
	union all select 45
	union all select 0
	union all select 0
	union all select 0
	union all select 58
	union all select 569
	union all select 2 
;

--test @AddedBy with different values 0, 1 and 2
declare @AddedBy integer;
set @AddedBy = 0;
--set @AddedBy = 1;
--set @AddedBy = 2;

select * from @TabNum
--for query I pass 0,1,2,
where (@AddedBy <> 0 or num >= 0) --for 0 return all,
and (@AddedBy <> 1 or num = 0) --for 1 return only 0 and 
and (@AddedBy <> 2 or num <> 0) --for 2 return only non 0 rows.
;
 
Share this answer
 
Comments
KuntalBose 6-Jan-15 0:27am    
Thanks @Member10454138
your solution work fine for me.It will be great if you explain the code for my knowledge.can you please explain the logic.
jaket-cp 6-Jan-15 5:09am    
No problem, glad to help.
Check out this link: http://weblogs.sqlteam.com/jeffs/archive/2003/11/14/513.aspx
it explains the technique very clearly, hope that helps out :)
KuntalBose 6-Jan-15 7:54am    
thanks for help and knowledge sharing.
jaket-cp 6-Jan-15 8:19am    
Share and share alike :)
Happy coding...
There are infinite values larger than 0 (zero) - as there are infinite values smaller than zero...So your basic problem is that you try to assign a larger-than-zero value to your variable, but didn't decided which one of them!
CreatedByClient = >0 is a logic error with and without the case in-between...
So you have to pick a value and assign it...
 
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