x

Sql 2005 Case Clause Query

DECLARE @Status INT 
SET @Status = 1 -- (can be 0,1,2)

SELECT * FROM Members 
WHERE Mem_Number in (
 SELECT MemberID 
 FROM GroupMember 
 WHERE GroupId = @GpId 
 AND IsCardNumber = 0) 
AND 
CASE WHEN @Status < 2 THEN Mem_Archeived = @Status
ELSE 1=1
END

--In this If @status variable is less than 2 then Mem_Archeived column is considered otherwise not.


Any help on this would be great!

Thanks Kapil

more ▼

asked Feb 25, 2010 at 10:31 AM in Default

Kapil Agarwal gravatar image

Kapil Agarwal
11 1 1 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

I think that should work

DECLARE @Status INT  
SET @Status = 1 -- (can be 0,1,2)

SELECT * FROM Members 
WHERE Mem_Number in (
 SELECT MemberID 
 FROM GroupMember 
 WHERE GroupId = @GpId 
 AND IsCardNumber = 0) 
AND 
Mem_Archeived = CASE WHEN @Status < 2 THEN @Status ELSE Mem_Archeived END
END

Scott

more ▼

answered Feb 25, 2010 at 11:34 AM

Scott Fisher gravatar image

Scott Fisher
46

(comments are locked)
10|1200 characters needed characters left

-- Try this

DECLARE @Status INT SET @Status = 1 -- (can be 0,1,2)

SELECT * FROM Members WHERE Mem_Number in ( SELECT MemberID
FROM GroupMember
WHERE GroupId = @GpId
AND IsCardNumber = 0) AND (Mem_Archeived = @Status OR @Status < 2)

more ▼

answered Mar 17, 2010 at 03:01 AM

Aditya Kumar gravatar image

Aditya Kumar
1

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x985
x369

asked: Feb 25, 2010 at 10:31 AM

Seen: 738 times

Last Updated: Feb 25, 2010 at 10:34 AM