Solved

case in where clasue

Posted on 2014-10-13
4
119 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: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 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

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

856 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