Solved

case in where clasue

Posted on 2014-10-13
4
115 Views
Last Modified: 2014-10-16
I am trying to write a query where I think I can use CASe statement . So what I want to do is say if the value is 0 look in one table other wise look in another table.

Some thing like this

CASE WHEN PROMOTYPE = 0
                   THEN
                        PT.newCode = 'XXX'
                   ELSE
                        PC.codeNAme = 'XCXX'
                   END
0
Comment
Question by:erikTsomik
4 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40378079
WHERE
    ( (PROMOTYPE = 0 AND PT.newCode = 'XXX') OR
      (PROMOTYPE <> 0 AND PC.codeNAme = 'XCXX') )

Or, to use a CASE:

WHERE 1 =
    CASE WHEN PROMOTYPE = 0 THEN CASE WHEN PT.newCode = 'XXX' THEN 1 ELSE 0 END
    ELSE CASE WHEN PC.codeNAme = 'XCXX' THEN 1 ELSE 0 END
    END
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 40378186
Thanks but it did not work, Now I get only with promotype =0 and none for promoType=1
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40378532
Assuming both PT and PC tables are referenced via FROM..JOIN, and PT.newCode and pc.CodeName have the same datatype..
SELECT blah, blah, blah, 
   CASE WHEN PROMOTYPE = 0 THEN PT.newCode 
   ELSE PC.codeNAme END as name_goes_here
FROM whatever

Open in new window

btw if it helps I have an article out there called SQL Server CASE Solutions that is an code-and-image-heavy tutorial on CASE blocks.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40379268
If this logic you have provided is correct:

CASE WHEN PROMOTYPE = 0
                   THEN
                        PT.newCode = 'XXX'
                   ELSE
                        PC.codeNAme = 'XCXX'
                   END

Then the conventional where conditions provided by Scott will match that logic:

WHERE (
         (PROMOTYPE = 0 AND PT.newCode = 'XXX')
      OR
         (PROMOTYPE <> 0 AND PC.codeNAme = 'XCXX')
      )

I cannot see any point to using a  case expression in the where clause for such a simple need.

+edit,
unless you have an expectation about NULLs in PROMOTYPE that isn't met by this logic.

It would be useful to provide data instead of just saying it doesn't work.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

19 Experts available now in Live!

Get 1:1 Help Now