Solved

case in where clasue

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why is this SQL bringing back extra rows? (parsing XML data) 4 38
StoredProcedure to JSON query faulty syntax 2 25
SQL 2014 missing dll from Bin? 3 33
SQL Syntax 6 31
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

749 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