Solved

Filtering out data in a WHERE clause based on CASE statement

Posted on 2007-12-03
5
213 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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!
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now