Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to use case in MSSQL 2005?

Posted on 2009-05-15
2
Medium Priority
?
507 Views
Last Modified: 2012-05-07
Hi

I have a select query which needs to take clientid as where condition. However, if the clientid=0, then it needs to select all result.


Select * from client where type=1 and clientid=2
 
if clientid=0 which I pass it from C#
then
Select * from client where type=1  //and clientid=0 would be taken out
 
I tried:
Select * from client where type=1 
case when (t.clientid ='0' then '' when t.clientid <> '0' then and t.clientid = '2') order by [id] desc
 
But, it said Incorrect syntax near the keyword 'case'.

Open in new window

0
Comment
Question by:techques
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24393069
this should be working:
Select * from client 
where type=1 
and ( @parameter = 0 or t.clientid = @parameter ) 
order by [id] desc

Open in new window

0
 

Author Closing Comment

by:techques
ID: 31581824
Excellent!! Thanks
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Integration Management Part 2
Screencast - Getting to Know the Pipeline

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question