Advertisement

09.20.2007 at 11:57AM PDT, ID: 22842377
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

T-SQL CASE statement in the WHERE clause of a query

Asked by cuziyq in MS SQL Server

Tags: , , , ,

I am working on a report that pulls data from a stored procedure.  The application prompts the user for parameters and then passes them into the stored procedure so the result set can be tailored to the user's preferences.

In the parameters dialog, the user can select to filter by a minimum and maximum number of days or months.  (If you're wondering, It's a banking industry thing.  90 days and 3 months are two entirely different things).  Anyway, months vs. days two mutually exclusive checkboxes (as opposed to a radio button) where the user can also choose not to filter by date range at all.

In my WHERE clause, I want to be able to do something like this:
SELECT <some stuff>
FROM <a bunch of tables>
WHERE <filtering done by other parameters> AND
        (IF @P1 = 1 OR @P2 = 1 THEN
                CASE (@P1 = 1)
                        THEN <call some function>
                CASE (@P2 = 1)
                        THEN <call some other function>)

The idea here is to do different calculations depending on whether we're filtering by days or months.  The CASE statement shouldn't even evaluate at all if neither option is taken.   When Googling this on the web, I get mixed results.  Some sites say you can't do that, and other say you can.  If you can do this, what would be the appropriate syntax?  How would I avoid (or deal with) the situation where the CASE statement would make the WHERE clause evaluate to something like WHERE <yada> AND  (IF 1 THEN 1), which is total nonsense.

Oh, and BTW, I am using SQL Server 2000.Start Free Trial
[+][-]09.20.2007 at 12:00PM PDT, ID: 19930773

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: case, statement, where, clause, sql
Sign Up Now!
Solution Provided By: aneeshattingal
Participating Experts: 2
Solution Grade: A
 
 
[+][-]09.20.2007 at 12:26PM PDT, ID: 19930987

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.20.2007 at 12:32PM PDT, ID: 19931028

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.20.2007 at 02:39PM PDT, ID: 19931994

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906