Solved

SQL Case question

Posted on 2013-01-30
2
246 Views
Last Modified: 2013-01-31
Hi

I have an sql query with a few parameters. If one of these parameters is 0 I want to use a different where clause.


I'd to this like this

If @Param = 0
  Begin
    --SQL here with where clause
  End

Else

  Begin
    --SQL here with another where clause
  End

My question is this, can this be done in a single SQL statement with say a CASE When in the where clause?

Andy
0
Comment
Question by:Andy Green
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
Comment Utility
SELECT ..
FROM ..
WHERE (@param = 0 and somethingElsse = someotherValue ) or
 ( (@param <> 0 and  SomeOtherCondition )
0
 
LVL 3

Author Closing Comment

by:Andy Green
Comment Utility
Thats is so cool (& simple). Gonna be using it again.

Thanks
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Copy Database Wizard 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 article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

743 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

14 Experts available now in Live!

Get 1:1 Help Now