Solved

How to use case in MSSQL 2005?

Posted on 2009-05-15
2
501 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to "ASSIGN" a RecordID based on 1st character of a Field? 8 37
Need help with a query 3 48
SQL Encryption question 2 57
Where clause in stored procedure 8 58
I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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