Solved

How to use case in MSSQL 2005?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now