Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

case in where clasue

Posted on 2014-10-13
4
Medium Priority
?
124 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
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 66

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 49

Accepted Solution

by:
PortletPaul earned 2000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

661 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