Solved

Filtering out data in a WHERE clause based on CASE statement

Posted on 2007-12-03
5
218 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
  • 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

830 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