Solved

Complex conditional WHERE statement with CASE

Posted on 2008-09-29
5
203 Views
Last Modified: 2012-05-05
The query I'm trying to build is almost complete. The query is something like this:

SELECT *
FROM table
WHERE condition1
  AND field3 IN (CASE field2='ABC' THEN 'textvalue1', 'textvalue2', 'textvalue3' ELSE field3 END)

In normal language: If field2 contains the value 'ABC', then I want field3 to match any of three string values. If field2 doesn't contain the value 'ABC', field3 can be anything.

The following works (but is of no use to me):

SELECT *
FROM table
WHERE condition1
  AND field3 IN (CASE field2='ABC' THEN 'textvalue1'' ELSE field3 END)
0
Comment
Question by:VSdB
  • 4
5 Comments
 
LVL 3

Expert Comment

by:R_Janssen
ID: 22597090
Try encapsulating?
(CASE field2='ABC' THEN ('textvalue1', 'textvalue2', 'textvalue3') ELSE field3 END)?
 
No management studio at hand so can't test if this works. CASE's in SQL are limited so there is a chance you'll have to write it out a bit more detailed. aka. More if's and multiple select statements based on the information at that particular step.
 
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 22597119
SELECT *
FROM table
WHERE condition1
  AND (field3 = (CASE field2='ABC' THEN 'textvalue1' ELSE field3 END)
    or field3 = (CASE field2='ABC' THEN 'textvalue2' ELSE field3 END)
     or field3 = (CASE field2='ABC' THEN 'textvalue3' ELSE field3 END)
)
     

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 22597127
or

SELECT *
FROM table
WHERE condition1
  AND field3 in ( (CASE field2='ABC' THEN 'textvalue1' ELSE field3 END),
                         (CASE field2='ABC' THEN 'textvalue2' ELSE field3 END),
                         (CASE field2='ABC' THEN 'textvalue3' ELSE field3 END)
                       )
     
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 total points
ID: 22597144
or

SELECT *
FROM table
WHERE condition1
  AND (field2 != 'ABC'
     or (field2='ABC' and  field3 in ('textvalue1', 'textvalue2', 'textvalue3' ))
         )
     
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 22597256
Why only a B?  What else did you need?  If the answer was insufficient, please ask for further clarifications rather than assign a penalty grade
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Select latest stock count records 11 56
Insert from Stored Procedure where some field/s > 0 7 43
Alter an update query which rounds 7 31
SQL Error in WHERE Clause 5 39
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

22 Experts available now in Live!

Get 1:1 Help Now