Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 include Case condition in Where Clause

Author  Topic 

asif372
Posting Yak Master

100 Posts

Posted - 2011-11-24 : 04:33:12
Dear,
I want to include condition in Where Clause
My Query is this but Case is not Working

DECLARE @Emp NVARCHAR(50)
SET @Emp = 40

Select * from Employee
WHERE
(Case When @EMP = 40 then Group = 1
When @EMP = 42 then Group = 2
)

Kindly Help
Thanks

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-11-24 : 04:35:46
DECLARE @Emp NVARCHAR(50)
SET @Emp = 40

Select * from Employee
WHERE Group = Case When @EMP = 40 then Group = 1 When @EMP = 42 then Group = 2 END
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-11-24 : 04:38:33
[code]
Select * from Employee
WHERE Group =
(
Case When @EMP = 40 then Group = 1
When @EMP = 42 then Group = 2
end
)[/code]

OR

[code]
Select *
from Employee
where (@EMP = 40 AND Group = 1)
OR (@EMP = 42 AND Group = 2)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

asif372
Posting Yak Master

100 Posts

Posted - 2011-11-24 : 04:47:33
Dear,
i dont want to search for 1 field i want conditions to search from different fields. when user select 40 it should search from group
and when user select 42 it should search from group2

Like this

(Case When @EMP = 40 then Group = 1
When @EMP = 42 then Group2 = 1
)

Kindly Help
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-11-24 : 05:02:26
Did you try the given solutions or do you think you are able to see - without trying - that they would not work?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

asif372
Posting Yak Master

100 Posts

Posted - 2011-11-24 : 07:43:26
Dear,
I want to include condition in Where Clause
when @EMP = 40 then it should Group1 = 1
Else
Group2 = 1

My Query is this but Case is not Working

DECLARE @Emp NVARCHAR(50)
SET @Emp = 40

Select * from Employee
WHERE
(Case When @EMP = 40 then Group1 = 1
When @EMP = 42 then Group2 = 1
)

Kindly Help
Thanks
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-11-24 : 07:49:17
DECLARE @Emp NVARCHAR(50)
SET @Emp = 40

Select * from Employee
WHERE Case When @EMP = 40 then Group1 When @EMP = 42 then Group2 END = Case When @EMP = 40 then 1 When @EMP = 42 then 2 END
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-11-24 : 07:49:33
Try this please:

Select * from Employee

WHERE
Group1 = CASE WHEN @EMP = 40 THEN 1 ELSE Group1 END
AND
Group2 = CASE WHEN @EMP = 42 THEN 1 ELSE Group2 END



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -