Solved

How to use case in MSSQL 2005?

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

624 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