Solved

SQL Case question

Posted on 2013-01-30
2
253 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
ID: 38837415
SELECT ..
FROM ..
WHERE (@param = 0 and somethingElsse = someotherValue ) or
 ( (@param <> 0 and  SomeOtherCondition )
0
 
LVL 3

Author Closing Comment

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

Thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access denied running PowerPivot -SQL Server 2014 on Windows Server 2012 10 28
script to send html report 3 30
Slow Connectivity over ODBC 8 32
convert null in sql server 12 31
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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

816 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