Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use case in MSSQL 2005?

Posted on 2009-05-15
2
Medium Priority
?
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
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 …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

688 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