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

x
?
Solved

Filtering out data in a WHERE clause based on CASE statement

Posted on 2007-12-03
5
Medium Priority
?
222 Views
Last Modified: 2010-04-21
Hello Gurus,
In my SELECT statement I have several CASE statements.  The 'ELSE' in one statement = NULL.  I do not want to select the data where NULL is returned.  For example, in the CASE statement, I am managing the company name from several different data sources with the ELSE = NULL.  There are some instances in a specific data source where there is a data record and the 'cleansed' company name returned is NULL.  I want to exclude that data from the SELECT.  I'm assuming I should be able to do that in the WHERE clause but have been unsuccessful.  Your assistance is greatly appreciated.

0
Comment
Question by:hypermac
[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
  • 3
5 Comments
 
LVL 1

Expert Comment

by:wizengamot
ID: 20399932
When you filter out NULL values the syntax is as follows in the code block below.  I hope this is what your looking for, if not, then please elaborate a bit more and I will do my best to assist further.

Please note the lack of =Null as this syntax is not supported.  An object cannot be Equal to Null because Null is Nothing and an object that is equal to by definition must have a value (the concept that it is equal means that it is equal to something other than nothing)..... This concept is one of the hardest to wrap your head around when dealing with NULL in a database.  If you get that, your well on your way to being a guru yourself.

SELECT * FROM tbSampleTable WHERE NOT SampleField IS NULL

Open in new window

0
 

Author Comment

by:hypermac
ID: 20400013
Thanks for such a quick.  Here's the CASE statement.
(CASE WHEN aa.ROW_SOURCE_DESC = 'MFS' AND CONFORMED_COMPANY_NUMBER IS NOT NULL THEN'Co'||LPAD(bb.CONFORMED_COMPANY_NUMBER,3,'0')
        WHEN aa.ROW_SOURCE_DESC IN ('ARTESIA', 'ARTESIA_OTHER') THEN 'Co'||LPAD(bb.COMPANY_NUMBER,3,'0')
        ELSE NULL
        END) AS COMPANY,
I do not want the query to return any records where the COMPANY is NULL.  How do I add this criteria to my WHERE clause?  Is that the correct place to do it?  
0
 
LVL 11

Accepted Solution

by:
yuching earned 2000 total points
ID: 20400248
You have 2 options
1.) Include the case when in where query
WHERE (CASE WHEN aa.ROW_SOURCE_DESC = 'MFS' AND CONFORMED_COMPANY_NUMBER IS NOT NULL THEN'Co'||LPAD(bb.CONFORMED_COMPANY_NUMBER,3,'0')
        WHEN aa.ROW_SOURCE_DESC IN ('ARTESIA', 'ARTESIA_OTHER') THEN 'Co'||LPAD(bb.COMPANY_NUMBER,3,'0')
        ELSE NULL
        END) IS NOT NULL

2.) Or if lazy to write because too many case when, then you can select from the subquery eg.

SELECT Company
FROM
(
       SELECT (CASE WHEN aa.ROW_SOURCE_DESC = 'MFS' AND CONFORMED_COMPANY_NUMBER IS  
            NOT NULL THEN'Co'||LPAD(bb.CONFORMED_COMPANY_NUMBER,3,'0')
            WHEN aa.ROW_SOURCE_DESC IN ('ARTESIA', 'ARTESIA_OTHER') THEN 'Co'||LPAD
           (bb.COMPANY_NUMBER,3,'0')
          ELSE NULL
           END)
         FROM yourtable
) WHERE Company IS NOT NULL
0
 

Author Comment

by:hypermac
ID: 20403532
Thanks yuching!  For some reason I thought I couldn't use a CASE statement in a WHERE clause.  DUH!
0
 

Author Closing Comment

by:hypermac
ID: 31412482
Got me where I needed to go!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

721 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